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

1 view (last 30 days)
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.

Answers (1)

Oleg Komarov
Oleg Komarov on 9 Feb 2011
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

Categories

Find more on Structures in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!