array subscripting

I have a struct variable defined as follows:
my_data(3) = struct('x',[],'y',[],'f',[])
for i=1:3
my_data(i).x = [1 2 3]*i;
my_data(i).y = [1 2 3]*i;
my_data(i).f = [1 2 3;4 5 6;7 8 9]*i;
end
Actually f is a matrix (N×M) which holds the value of a function for each (x,y) and stores its value in different measures. x and y are vectors (N×1). Therefore, my_data(1).f(:,2) means the value of the function in the second measure for experiment#1. I am trying to assign f to another variable (like new_f) columnwise. In other words, I am trying to assign the first columns of all the experiments (from 1 to 3) to the first column of new_f and so on. The following assigns each column of f but can I modify it a little bit to assign all the columns to new_f:
column1=arrayfun(@(S) S(:).f(:,1), my_data,'UniformOutput', false);
column2=arrayfun(@(S) S(:).f(:,2), my_data,'UniformOutput', false);
column3=arrayfun(@(S) S(:).f(:,3), my_data,'UniformOutput', false);
how can I combine all the above assignments into one neat assignment?
Thank you very much for your quick reply.

 Accepted Answer

T = vertcat(S.f);
T = mat2cell(T, size(T,1), ones(1,size(T,2)));
[column1, column2, column3] = deal(T{:});

6 Comments

AP
AP on 2 Jun 2011
Thanks Walter for your answer. Actually, I don't have 3 columns. I just gave an example to clarify my question. It is 5227 columns. How can I modify your code to get that?
Matt Fig
Matt Fig on 2 Jun 2011
Isn't what you want just given by Walter's first line?
new_f = vertcat(my_data.f)
AP
AP on 2 Jun 2011
I modified it to cols=deal([T{:}]) and it worked.
Matt Fig
Matt Fig on 2 Jun 2011
Notice that the result of your above modification match exactly my suggestion above, without the extra two lines of code!
I was following the code sample that showed assignment in to separate variables. If you want them all in one matrix then the first line is all you need.
AP
AP on 2 Jun 2011
awesome. Walter and Matt thank you for all your help :)))))))))))

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!