1

I have a structure which contains two fields one of them contains vectors,example:

myStruct(1).f1 = val1;
myStruct(1).f2 = [elt1 elt2 elt3];

I want to find the indices of the elements of myStruct where elt1 == valA and elt2 == valB

2 Answers 2

1

One way to do this would be:

% Create a matrix whose rows are the f2 vectors in the struct array
A = cell2mat({myStruct.f2}.');

% Find which rows match your conditions
Matches = (A(:,1) == valA) & (A(:,2) == valB);

% (If required: convert logical vector to indices)
Indices = find(Matches);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! it works without even converting the logical vector to indices
0

Another way to do this:

F2 = reshape([myStruct.f2],3,[]); 

Indices = find(((F2(1,:) == valA) & (F2(2,:) == valB)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.