Got Questions? Get Answers.
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:
Compare Structure Entries

Subject: Compare Structure Entries

From: Zeph Davies

Date: 31 Dec, 2012 04:08:12

Message: 1 of 3

lets say i have a structure:

A(1).a = 1
A(1).b = 2

A(2).a = 2
A(2).b = 3

A(3).a = 3
A(3).b = 4

and

B(1).a = 2
B(1).b = 3

Clearly B(1) = A(2).

without the use of a loop, can i get the comparative logic array:
[0 1 0]

I know the solution:
for i = 1:length(A)
isequal(A(i),B)
end
But this uses a loop. Is it possible without a loop?

ismember doesn't like structures.

Thanks,

Zeph

Subject: Compare Structure Entries

From: Zeph Davies

Date: 31 Dec, 2012 04:16:15

Message: 2 of 3

"Zeph Davies" wrote in message <kbr33c$nvs$1@newscl01ah.mathworks.com>...
> lets say i have a structure:
>
> A(1).a = 1
> A(1).b = 2
>
> A(2).a = 2
> A(2).b = 3
>
> A(3).a = 3
> A(3).b = 4
>
> and
>
> B(1).a = 2
> B(1).b = 3
>
> Clearly B(1) = A(2).
>
> without the use of a loop, can i get the comparative logic array:
> [0 1 0]
>
> I know the solution:
> for i = 1:length(A)
> isequal(A(i),B)
> end
> But this uses a loop. Is it possible without a loop?
>
> ismember doesn't like structures.
>
> Thanks,
>
> Zeph




ismember([A.a],B.a) & ismember([A.b],B.b)

This works, but what if i have many fields?

Zeph

Subject: Compare Structure Entries

From: Bruno Luong

Date: 31 Dec, 2012 08:18:11

Message: 3 of 3

"Zeph Davies" wrote in message <kbr33c$nvs$1@newscl01ah.mathworks.com>...

> I know the solution:
> for i = 1:length(A)
> isequal(A(i),B)
> end
> But this uses a loop. Is it possible without a loop?

arrayfun(@(x) isequal(x,B), A)

But intrinsically this solution is not better than the for-loop.

Bruno

Tags for 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