finding index to a vector in structure

조회 수: 1 (최근 30일)
James McDaniel
James McDaniel 2015년 10월 1일
댓글: Jos (10584) 2015년 10월 1일
Consider the following example:
f = struct
f(1).x = 5;
f(2).x = 1:10;
f(3).x = 1;
f(4).x = 2;
I wish to find an automated way of finding which element of the field x is a vector. In the example above, the answer is element 2. Thank you!

채택된 답변

the cyclist
the cyclist 2015년 10월 1일
I could not figure out a vectorize way to do this, but perhaps this loop is good enough:
n = size(f,2);
isVectorF = false(1,n);
for nv = 1:n,
isVectorF(nv) = not(isscalar(f(nv).x));
end
isVectorF is a logical with 0 = scalar and 1 = vector.
  댓글 수: 1
Jos (10584)
Jos (10584) 2015년 10월 1일
isVectorF = arrayfun(@(k) isvector(f(k).x) && numel(f(k).x)>1 ,1:numel(f))

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by