I actually want to add a screenshot of the m. script because the code didn't show up correctly.
For Loop and final output matrix
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
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
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!