Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Finding an element in a data structure

Subject: Finding an element in a data structure

From: Liana

Date: 1 Apr, 2011 04:53:04

Message: 1 of 3

Hello!

Below is a simplified example of what I'm trying to do. So, there is a data structure 's' with three elements s(1), s(2) and s(3). Each element is described by two fields - 'path' and 'length'.
s(1) = struct('path', {curve1}, 'length', {clength1});
s(2) = struct('path', {curve2}, 'length', {clength2});
s(3) = struct('path', {curve3}, 'length', {clength3});

I'd like to find an element AND its index that corresponds to the minimal 'length' value. I'm trying the following but it doesn't work:
shortest_path = min(s.length);
Please give an idea of how to do that.

Thanks.

Subject: Finding an element in a data structure

From: Liana

Date: 1 Apr, 2011 05:51:05

Message: 2 of 3

I figured it out.
s = struct('path', {}, 'length', {});
 s(1).path = curve1; s(1).length = clength1;
 s(2).path = curve2; s(2).length = clength2;
 s(3).path = curve3; s(3).length = clength3;
len = s(:).slength;
shortest_path = min(len);
ind = find(min(len));

"Liana" wrote in message <in3lng$b1o$1@fred.mathworks.com>...
> Hello!
>
> Below is a simplified example of what I'm trying to do. So, there is a data structure 's' with three elements s(1), s(2) and s(3). Each element is described by two fields - 'path' and 'length'.
> s(1) = struct('path', {curve1}, 'length', {clength1});
> s(2) = struct('path', {curve2}, 'length', {clength2});
> s(3) = struct('path', {curve3}, 'length', {clength3});
>
> I'd like to find an element AND its index that corresponds to the minimal 'length' value. I'm trying the following but it doesn't work:
> shortest_path = min(s.length);
> Please give an idea of how to do that.
>
> Thanks.

Subject: Finding an element in a data structure

From: Matt J

Date: 1 Apr, 2011 09:46:04

Message: 3 of 3

"Liana" wrote in message <in3p48$6kr$1@fred.mathworks.com>...
> I figured it out.
> s = struct('path', {}, 'length', {});
> s(1).path = curve1; s(1).length = clength1;
> s(2).path = curve2; s(2).length = clength2;
> s(3).path = curve3; s(3).length = clength3;
> len = s(:).slength;
> shortest_path = min(len);
> ind = find(min(len));
==============================

No, that doesn't look right. It should have been enough to do

[shortest_path,ind] = min([s.length]);

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us