[英]Access elements of a vector in a structure array MATLAB
我有一个包含两个字段的结构,其中一个包含向量,例如:
myStruct(1).f1 = val1;
myStruct(1).f2 = [elt1 elt2 elt3];
我想找到myStruct
元素的索引,其中elt1 == valA
和elt2 == 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)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.