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

Afaf
  • 59
  • 5

2 Answers2

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);
Harry
  • 884
  • 1
  • 8
  • 19
0

Another way to do this:

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

Indices = find(((F2(1,:) == valA) & (F2(2,:) == valB)));
Mansour Torabi
  • 411
  • 2
  • 6