Is there a version of STRUCTFUN that operates on structure fields that are matrices instead of scalars?

1 回表示 (過去 30 日間)
My structure S that has two fields A, and B, both of which are vectors. I would like to be able to index into the the structure to pick out any four elements, and find the unique elements in B.
For example, given the structure S defined by: S(1).A= [ 1 2] ; S(1).B= [ 2 4 1] ; S(2).A= [ 2 2] ; S(2).B= [ 2 1] ; S(3).A= [ 0 2] ; S(3).B= [ 5] ; S(4).A= [ 1 4] ; S(4).B= [ 1 4 3 10] ; S(5).A= [ 3 2] ; S(5).B= [ 2 4 7.5 9 11] ;
I would like to be able to say: VECTORSTRUCTFUN( @(x) union( x.B), S([1 2 4 5]))
and get [1 2 3 4 7.5 9 10 11] which are the union of the four vectors.

回答 (1 件)

Oleg Komarov
Oleg Komarov 2011 年 2 月 9 日
You should go a slightly different way:
S(1).A = [ 1 2];
S(1).B = [ 2 4 1];
S(2).A = [ 2 2];
S(2).B = [ 2 1];
S(3).A = [ 0 2];
S(3).B = [ 5];
S(4).A = [ 1 4];
S(4).B = [ 1 4 3 10];
S(5).A = [ 3 2];
S(5).B = [ 2 4 7.5 9 11];
% Retrieve unique values from the union of S(1).B, S(2).B, ...
unique([S([1 2 4 5]).B])
Oleg

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by