Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7763479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:40:10+00:00 2026-06-01T14:40:10+00:00

Apologies if this is a stupid question, I’m relatively new to Matlab. I’ve got

  • 0

Apologies if this is a stupid question, I’m relatively new to Matlab.

I’ve got an array of rectangles of class Rectangle with properties minx, miny, maxx, maxy, which represent the corner coordinates.

I’m trying to get the index of the top-left rectangle in the array.

I can loop through the objects to get the object that corresponds to the minimum x and y coordinates, but this doesn’t seem like a very matlabic (matlabian? doesn’t sound nearly as good as pythonic) way to do it.

minx = -1
miny = -1
tl_rect_id = 0

for id = 1:num_objects
    if ((rectangles(id).minx < minx || minx == -1) && (rectangles(id).miny < miny || miny == -1)) 
        tl_rect_id = id
        minx = rectangles(id).minx
        miny = rectangles(id).miny

    end

    % plot coordinates of top left corners
    plot(rectangles(id).minx,rectangles(id).miny,'+r')
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:40:12+00:00Added an answer on June 1, 2026 at 2:40 pm

    You don’t need to use arrayfun to operate on arrays of objects in matlab. There’s a very useful shorthand for getting an array of properties out of an array of objects:

    [rectangles.minx]
    

    That yields all rectangle’s minx in an array.

    So to know which is the point closest to the origin, I would calculate the good ol’ euclidean distance to the origin. With the vectors at hand, that’s REALLY simple.

    The euclidean distance is defined as follows:

    d(a,b) = sqrt( (a.x - b.x)^2 + (a.y - b.y)^2);
    

    to calculate it with your vectors:

    distances = sqrt([rectangles.minx].^2 + [rectangles.miny].^2)
    

    This will yield a vector with the distances of all points. Finding the minimum is trivial:

    [~, idx] = min(distances);

    The min function returns a 1×2 array, the first position is the minimum value, the second is the index. I’ve used the matlab notation [~, idx] to state that I’m not interested in the first return value, and the second should be stored on the variable idx.

    I’ve written an example in which I have created my rectangle class only to test it, but it will work with your class as well. Below are the codes for the class I’ve defined and the code that calculates the point closest to (0,0).

    Run it to play with the idea and adapt it to your needs 🙂

    Test Class definition (save that in a file called Rectangle.m):

    classdef Rectangle
        properties
            minx;
            miny;
        end
        methods
            function obj = Rectangle(v1,v2)
             if nargin > 1
                obj.minx = v1;
                obj.miny = v2;
             end
          end
        end
    end
    

    Code

    clear all;
    numRect = 100;
    rect_array = Rectangle(numRect);
    
    % initialize rectangles
    for n=1:numRect
        r = Rectangle;
        r.minx = 100*rand(1,1);
        r.miny = 100*rand(1,1);
        rect_array(n) = r;
    end
    
    % find point closest to the origin
    
    [~, idx] = min(sqrt([rect_array.minx].^2 + [rect_array.miny].^2));
    
    close all;
    figure;
    % plot the points in blue
    plot([rect_array.minx],[rect_array.miny],'b.');
    hold on;
    % mark a red cross on the point closest to the origin
    plot(rect_array(idx).minx, rect_array(idx).miny, 'rx');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apologies if this is a stupid question as Im completly new to JAvascript and
I'm new to Python so apologies in advance if this is a stupid question.
I'm still fairly new to symfony, so my apologies if this is a stupid
Apologies if this is a stupid question - I'm an Android and Ant newbie.
My apologies if this is a stupid question, however I can't find any information
Apologies for the massively stupid question but I am curious how to get this
Apologies if this is a stupid/easy question, but still getting used to everything in
Apologies if this ends up being a stupid question, but I was just wondering
No idea how to go about this. Apologies if it's a stupid question (probably
Apologies if this is too ignorant a question or has been asked before. A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.