Writing outputs from a 3D matrix loop

조회 수: 2 (최근 30일)
Robert
Robert 2015년 11월 5일
답변: Federico Becattini 2015년 11월 5일
I have a 3D matrix 35,43,144
I want to run the following code such that it performs the function element by element through the 3rd dimension (using 144 data points) I want this to be done for every pixel giving me 1505 outputs (35x43)
p = load ('JPLCSRmean.mat')
GRACE = p.datamean
ncol = size(GRACE,2);
for i = 1:144
for j = 1:43
for k = 1:35
[c,l] = wavedec(GRACE(i,j,k),4,'dmey');
al = appcoef(c,l,'dmey',1);
%[cD1,cD2,cD3,cD4,cD5,cD6] = detcoef(c,l,[1,2,3,4,5,6]);
D = [];
%D(:,6)= wrcoef('a',c,l,'dmey',6);
%D(:,5) = wrcoef('a',c,l,'dmey',5);
D(:,4) = wrcoef('a',c,l,'dmey',4);
D(:,3) = wrcoef('a',c,l,'dmey',3);
D(:,2) = wrcoef('a',c,l,'dmey',2);
D(:,1) = wrcoef('a',c,l,'dmey',1);
filename = sprintf('%s_%04f.txt', 'GRACE_APROXIMATIONS', k)
csvwrite(filename,D)
end
end
end
I think it is running allright but the outputs aren't saving properly, I think it is to do with where I have 'k' in this line
filename = sprintf('%s_%04f.txt', 'GRACE_APROXIMATIONS', k)
What can I replace 'k' with to give me 1505 outputs of 35x43? At the moment I am just getting 35 files that save over each other
I have tried adding another for loop with m = 1:1505 and replacing k with m but this has not helped.
  댓글 수: 1
Walter Roberson
Walter Roberson 2015년 11월 5일
Why 1505 outputs that size rather than 144 outputs?

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

답변 (1개)

Federico Becattini
Federico Becattini 2015년 11월 5일
you are giving a filename using only k to make different files. Of course this does not consider the value of i and j and hence you are creating 35 files that are overwritten 1505 times. To make more outputs you have to create files with names that take into account both i and j. Something like
filename = sprintf('%s_%d_%d.txt', 'GRACE_APROXIMATIONS', i, j)
or
filename = sprintf('%s_%d_%d_%d.txt', 'GRACE_APROXIMATIONS', i, j, k)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by