Store values i Matrix from a loop

조회 수: 2 (최근 30일)
mads skibsted
mads skibsted 2024년 3월 7일
댓글: Rik 2024년 3월 7일
Hi,
I want to store the interations of the the loop into a matrix with maxima and minima.
How can I do that?
%% Import data
ta0tcy100 = importdata("C:\UndrianedMon\triax_hypoplasticity-print-out\EALL_element_1.dat").data;
ta0tcy95 = importdata("C:\UndrianedMon\triax_hypoplasticityta70-print-out\EALL_element_1.dat").data;
ta0tcy90 = importdata("C:\UndrianedMon\triax_hypoplasticityta100-print-out\EALL_element_1.dat").data;
%% Finding devatoric stresses, mean pressure, shear strains and time
sig1 = [ta0tcy100(:,3) ta0tcy95(:,3) ta0tcy90(:,3)];
sig2 = [ta0tcy100(:,4) ta0tcy95(:,4) ta0tcy90(:,4)];
sig3 = [ta0tcy100(:,2) ta0tcy95(:,2) ta0tcy90(:,2)];
eps1 = [ta0tcy100(:,9) ta0tcy95(:,9) ta0tcy90(:,9)];
eps2 = [ta0tcy100(:,10) ta0tcy95(:,10) ta0tcy90(:,10)];
eps3 = [ta0tcy100(:,8) ta0tcy95(:,8) ta0tcy90(:,8)];
time = [ta0tcy100(:,1) ta0tcy95(:,1) ta0tcy90(:,1)];
q = (sig1-sig3)/2;
p = (sig1+sig3)/2;
s = eps1-eps3;
t = time;
%% Finding average and cyclic shear strains and stresses
Maxima = cell(1, size(q, 2));
MaxIdx = cell(1, size(q, 2));
Minima = cell(1, size(q, 2));
MinIdx = cell(1, size(q, 2));
for i = 1:size(q,2)
[maxima,maxIdx] = findpeaks(s(:,i));
[minima,minIdx] = findpeaks(-s(:,i));
Maxima{i} = maxima;
MaxIdx{i} = maxIdx;
Minima{i} = -minima;
MinIdx{i} = minIdx;
end
  댓글 수: 1
Rik
Rik 2024년 3월 7일
You're already storing every iteration. What exactly is your problem?

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

답변 (1개)

Joe Vinciguerra
Joe Vinciguerra 2024년 3월 7일
Depending on what exactly you're trying to do, put inside your for loop...
1) If you just want to store an array of i values you can add
ii{i} = i;
or
ii(i) = i;
2) If you want to store i as a vector within the same array as the other variables you could change, for example
Maxima{i} = maxima;
to be
Maxima{i,1} = maxima;
and add
Maxima{i,2} = i;
(or swap the index positions for a horizontal versus vertical array)
  댓글 수: 1
Rik
Rik 2024년 3월 7일
Just a note: Maxima{i,2} = i; will not add a lot of value, since it can be replaced with this:
Maxima = cell(3,1);
for n=1:size(Maxima,1)
Maxima{n,1} = n;
end
Maxima(:,2) = num2cell(1:size(Maxima,1));
Maxima
Maxima = 3×2 cell array
{[1]} {[1]} {[2]} {[2]} {[3]} {[3]}

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

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by