访问结构数组 MATLAB 中向量的元素

Access elements of a vector in a structure array MATLAB

我有一个包含两个字段的结构,其中一个包含向量,例如:

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

我想找到 myStruct 的元素索引,其中 elt1 == valAelt2 == valB

一种方法是:

% 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);

另一种方法:

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

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