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

2 次查看(过去 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 CenterFile Exchange 中查找有关 Structures 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by