필터 지우기
필터 지우기

For Loop and final output matrix

조회 수: 1 (최근 30일)
Amine Ben Ayara
Amine Ben Ayara 2016년 10월 4일
편집: Andrei Bobrov 2016년 10월 5일
Hello Matlab Wizards, I hope everyone is doing wonderful. I have written a code that has an outside loop that consists of basically increasing the values of all variables in one of the input matrices within the second column incrementally from 0:750. The end product is a matrix that is computed based on some other matrices. What I would like help with is; I need to get some intermediate matrices out everytime an iteration takes place( i.e., if the loop goes 10 times, I need to save a particular intermediate matrix ten times) because I have to verify something. This is an example of my code:
function [CrUnit,AgrCarbonSimuN1,CR9,AgrCarbonSimuN2,AgrCarbonSimuN3,MN, MSA1,...
MSA2, MSA3] = CarbonProject(X, Lam,Y,UnitArea,ALLC3P,n,m)
%Carbon Storage convereted by Spatial Unit by period ( Period 1 through 3)
Xv=X
for k=0:750;
X(:,2)=X(:,2)+k;
[Mtest,MN, MN1, MN2, MN3, Mtest1, Mtest2] = Predict_all( X,Lam,Y)
[MS1, MS2,MS3,MSA1,MSA2, MSA3, MSA ] = MatrixCountnew(Mtest1,Mtest2,...
MN1, MN2, MN3,Y,UnitArea,n)
[CS,CS3030,CSLS,SUMCSLS,CrUnit] = CarbonPixels( ALLC3P,MSA,UnitArea,m );
for j=1:44040 ;
CR9(j,1)=CrUnit(:,:,j);
end
Period1Carbon= CR9(1:14680);
Period2Carbon= CR9(14681:29360);
Period3Carbon= CR9(29361:44040);
for cr7=1:14680;
[AgrCarbon1(cr7,:)]=[Period1Carbon(cr7,:)];
[AgrCarbon2(cr7,:)]=[Period2Carbon(cr7,:)];
[AgrCarbon3(cr7,:)]=[Period3Carbon(cr7,:)];
end
AgrCarbonSimuN1(1:14680,k+1)=AgrCarbon1;
AgrCarbonSimuN2(1:14680,k+1)=AgrCarbon2;
AgrCarbonSimuN3(1:14680,k+1)=AgrCarbon3;
X=Xv;
end
So basically, by the end of each iteation(i=0:750) I would like to have these matrices: MN1, and MSA1 not just when all the loop is done 750 times. I hope I made some sense.
  댓글 수: 4
dpb
dpb 2016년 10월 4일
What's the dimension of the two arrays of interest now?
Andrei Bobrov
Andrei Bobrov 2016년 10월 5일
CrUnit - array with size: (1 x 1 x 44040)?

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

답변 (2개)

Benjaminas Marcinkevicius
Benjaminas Marcinkevicius 2016년 10월 5일
Example how i usually do it, a big ugly, but works if understood your problem correctly. you end up with cell array filled with your desired matrix.
%code
a = cell(20,1); % create cell array
for j=1:20
if (mod(j,10) == 0) % condition how often do you want to dump your matrix
a{j,1}= [1 1 1 1 1];
end
end
a = a(~cellfun('isempty',a)) % clear empty cell arrays
  댓글 수: 1
Amine Ben Ayara
Amine Ben Ayara 2016년 10월 5일
편집: Andrei Bobrov 2016년 10월 5일
Good morning Ben, I replied but for some reason, it did not post to your answer as well. Here is what I wrote:
Thank you so much for taking the time to help with this. You wrote a really elaborate code that I am having a hard time using because of the variety of the dimensions of my output matrices. lol Let me make my question simpler and clearer if I may. The objective is to get a set of matrices( one set of output) after every iteration is done (i.e, if i=0:10, then after 11 loops I should have 11 sets of output matrices). This is simply done by increasing one set of variables from one input matrix X(:,2) incrementally by 1 from 0 to 10. So for example, MN is a 3D matrix that has 14680 (5*5) matrices in it. So I need to have one set of MN after each iteration is done so basically 11 MN. here is a more simplified version of my code only focusing on few matrices.
function [MNPr1,MSA1,MSA1Trans] = Prb_Trans_CR7(X, Lam,Y,UnitArea,n,m)
%Carbon Storage convereted by Spatial Unit by period ( Period 1 through 3)
Xv=X
for k=0:10;
[Mtest,MN, MN1, MN2, MN3, Mtest1, Mtest2] = Predict_all( X,Lam,Y)
[ MS1, MS2,MS3,MSA1,MSA2, MSA3, MSA ] =...
MatrixCountnew(Mtest1,Mtest2, MN1, MN2, MN3,Y,UnitArea,n)
MNPr1(k)=MN1;
MSA1Trans(k)=MSA1;
X=Xv;
end

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


Andrei Bobrov
Andrei Bobrov 2016년 10월 5일
편집: Andrei Bobrov 2016년 10월 5일
Variant:
function [CrU,AgrCarbonSimu,MN,MN1,MSA1, MSA2, MSA3]...
= CarbonProject(X, Lam,Y,UnitArea,ALLC3P,n,m)
%Carbon Storage convereted by Spatial Unit by period ( Period 1 through 3)
AgrCarbonSimu = zeros(14680,751,3);
xx = bsxfun(@plus,X(:,2),0:750);
MN = cell(751,1);
MN1 = cell(751,1);
MSA1 = cell(751,1);
MSA2 = cell(751,1);
MSA3 = cell(751,1);
CrU = zeros(14680,751);
for k = 1:751;
[Mtest,MN{k}, MN1{k}, MN2, MN3, Mtest1, Mtest2] = Predict_all(xx(:,k),Lam,Y)
[MS1, MS2,MS3,MSA1{k},MSA2{k}, MSA3{k}, MSA ]...
= MatrixCountnew(Mtest1,Mtest2,MN1{k}, MN2, MN3,Y,UnitArea,n);
[CS,CS3030,CSLS,SUMCSLS,CrU(:,k)] = CarbonPixels( ALLC3P,MSA,UnitArea,m );
AgrCarbonSimu(:,k+1,:) = reshape(CrU(:,k),[],3);
end
  댓글 수: 1
Amine Ben Ayara
Amine Ben Ayara 2016년 10월 5일
편집: Andrei Bobrov 2016년 10월 5일
Hello Andrei, Thank you so much for taking the time to help with this. You wrote a really elaborate code that I am having a hard time using because of the variety of the dimensions of my output matrices. lol Let me make my question simpler and clearer if I may. The objective is to get a set of matrices( one set of output) after every iteration is done (i.e, if i=0:10, then after 11 loops I should have 11 sets of output matrices). This is simply done by increasing one set of variables from one input matrix X(:,2) incrementally by 1 from 0 to 10. So for example, MN is a 3D matrix that has 14680 (5*5) matrices in it. So I need to have one set of MN after each iteration is done so basically 11 MN. here is a more simplified version of my code only focusing on few matrices.
function [MNPr1,MSA1,MSA1Trans] = Prb_Trans_CR7(X, Lam,Y,UnitArea,n,m)
%Carbon Storage convereted by Spatial Unit by period ( Period 1 through 3)
Xv=X
for k=0:10;
[Mtest,MN, MN1, MN2, MN3, Mtest1, Mtest2] = Predict_all( X,Lam,Y)
[ MS1, MS2,MS3,MSA1,MSA2, MSA3, MSA ] = ...
MatrixCountnew(Mtest1,Mtest2, MN1, MN2, MN3,Y,UnitArea,n)
MNPr1(k)=MN1;
MSA1Trans(k)=MSA1;
X=Xv;
end
the beginning has Xv=X and closing the code has X=Xv; that's basically used to reset the values of X(:,2) after each iteration is done. I hope I explained better! THANK YOU so much

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by