Storing output from each FOR LOOP ititeration in MATLAB

Hello,
Say I have 3 matrix data files in a folder..
I have a function (clustering_coef_bu) which calculates the clustering coefficient of a 2D matrix (data; has the dimensions 512x512) file. The output vector of the function creates a 512x1 Matrix (Clustering Coefficient), in double format.
With the for loop below, for each matrix (data) I'm calculating the clustering coefficient. However, I am having difficulting being able to store the output clustering coefficient for each run of the for loop. It would be ideal to output the clustering coefficient of each matrix into one singular structure. I.e a cell array, which has the dimensions 512x3.
for k = 1:3
ClusteringCoefficient=clustering_coef_bu(data)
end
Anyhelp would be great. Thanks

 채택된 답변

Jan
Jan 2013년 8월 7일
편집: Jan 2013년 8월 7일
Or:
ClusteringCoefficient = zeros(512, 3);
my_data = cat(3, data1, data2, data3);
for k = 1:3
ClusteringCoefficient(:, k) = clustering_coef_bu(my_data(:,:,k));
end

댓글 수: 3

Thanks Simon.
I should of been more specific in saying I intend for the actual data themselves to be generated within the for loop. This is performed by a randomizer function, which randomizes an input data matrix.
The function is called as;
randomnetwork=randomizer(input)
But, can I apply a similar function as above to generate a 3D matrix. I.e
randomnetworkdata = zeros(512, 512, 3)
for k = 1:3
randomnetworkdata(:,:,k) = randomizer(data)
Then add your above code for the clustering coefficient calculation.
Yes, of course, this should work. You can simply try it to test if it works.
Thanks Simon, I have the correct code now.
randomnetworkdata = zeros(512, 512, 3);
ClusteringCoefficient = zeros(512, 3);
for k = 1:3
randomnetworkdata(:,:,k) = randomizer(s);
ClusteringCoefficient(:,k) = clustering_coef_bu(randomnetworkdata(:,:,k));
end

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

추가 답변 (1개)

David Sanchez
David Sanchez 2013년 8월 7일
ClusteringCoefficient = cell(3,1); % initialize empty cell array
my_data = zeros(512,512,3); % initialize empty matrix to hold your data
my_data(:,:,1) = data1;
my_data(:,:,2) = data2;
my_data(:,:,3) = data3; % your 3 matrices put together in a 3D matrix, you can adapt this last 4 lines to your data
for k = 1:3
ClusteringCoefficient{k} = clustering_coef_bu( my_data(:,:,k) );
end

카테고리

도움말 센터File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

질문:

2013년 8월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by