Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

i have a two dimensional matrix inside a loop i want to save the data matrix inside the loop so that each time the loop runs the data doesn't gets overwritten with the next one what should i do?

1 visualización (últimos 30 días)
the value inside the loop is a two dimensional matrix that i need save every time
  2 comentarios
Paolo
Paolo el 4 de Jul. de 2018
Editada: Paolo el 4 de Jul. de 2018
Initialize a cell array of size n, where n is the number of elements you want to save, before your loop, and store the results of your calculations within your loop in the cell array.
Can you show us your code?
OMKAR ACHARJEE
OMKAR ACHARJEE el 4 de Jul. de 2018
here i give you the code
clear all
load trmm;
load index_1;
data = trmm(:,1:10);
rain = data(:,:);
y = index_1(:,:);
for n = 1:size(trmm,2)
dataA=[];
for i=1:length(rain)
dataA=[dataA; (data(i,n))];
end
dataB=[];
for i=1:length(rain)-1
dataB=[dataB; sum(data(i:i+1,n))];
end
dataC=[];
for i=1:length(rain)-2
dataC=[dataC; sum(data(i:i+2,n))];
end
dataD=[];
for i=1:length(rain)-3
dataD=[dataD; sum(data(i:i+3,n))];
end
dataF=[];
for i=1:length(rain)-4
dataF=[dataF; sum(data(i:i+4,n))];
end
dataG=[];
for i=1:length(rain)-5
dataG=[dataG; sum(data(i:i+5,n))];
end
dataK=[];
for i=1:length(rain)-6
dataK=[dataK; sum(data(i:i+6,n))];
end
dataH=[];
for i=1:length(rain)-7
dataH=[dataH; sum(data(i:i+7,n))];
end
dataI=[];
for i=1:length(rain)-11
dataI=[dataI; sum(data(i:i+11,n))];
end
dataJ=[];
for i=1:length(rain)-15
dataJ=[dataJ; sum(data(i:i+15,n))];
end
% % Make padded array.....
M = nan(length(rain),10);
M(1:length(data),1) = dataA;
M(2:length(data),2) = dataB;
M(3:length(data),3) = dataC;
M(4:length(data),4) = dataD;
M(5:length(data),5) = dataF;
M(6:length(data),6) = dataG;
M(7:length(data),7) = dataK;
M(8:length(data),8) = dataH;
M(12:length(data),9) = dataI;
M(16:length(data),10) = dataJ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
year=y(:,1);
dataset=[year M];
[years, ~, gg] = unique(dataset(:, 1));
dataset1=[gg M];
MaxC=[];
for i=1:20
for k1 = 2:size(dataset,2)
maxd = max(dataset(gg==i,k1));
MaxCol(k1,:) = maxd;
end
MaxC=[MaxC MaxCol];
end
MaxC =(MaxC(2:end,:))';
mm=mean(MaxC,1);
stdev=std(MaxC,1);
recurrence=[2;5;10;25;50;100];
KT=[];
for i=1:length(recurrence)
gumbell_kt(i)=-(sqrt(6))/pi*(0.5772+log(log(recurrence(i)/(recurrence(i)-1))));
end
KT=[KT gumbell_kt];
KT=KT';
dataarrange=[mm' stdev'];
for i=1:length(dataarrange)
for j=1:length(KT)
table(i,j)=dataarrange(i,1)+KT(j).*dataarrange(i,2);
end
end
duration=[3;6;9;12;15;18;21;24;36;48];
for i=1:length(duration)
for j=1:length(KT)
intensity(i,j)=table(i,j)./duration(i);
end
end
plot(duration,intensity(:,1),'bd-',duration,intensity(:,2),'gd-',duration,intensity(:,3),'cd-',duration,intensity(:,4),'kd-',duration,intensity(:,5),'md-',duration,intensity(:,6),'yd-');
end
i want to store the values of intensity and as well as save the resulting plots for every value of n which is the outer loop index and runs from 1 to 10 otherwise the values gets over written... please help me out....

Respuestas (2)

Jan
Jan el 4 de Jul. de 2018
To store 100 matrices of size 3x4:
n = 100;
Result = zeros(3, 4, n);
for k = 1:n
Result(:, :, k) = rand(3, 4); % Replace rand by your data
end
Or if the matrices have different sizes, use a cell array:
n = 100;
Result = cell(1, n);
for k = 1:n
Result{k} = rand(2, k);
end

Kulan Sinclair
Kulan Sinclair el 4 de Jul. de 2018
you can do it as either a cell array
for i = 1:10
matrix = rand(10,10);
matrixcellarray{i} = matrix;
end
or a structure
matrixName = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
for i = 1:numel(matrixName)
matrix = rand(10,10);
matrixStructure.(matrixName{i}) = matrix;
end
which is more useful depends on what you are trying to do with the data

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by