Clear Filters
Clear Filters

Store values i Matrix from a loop

2 views (last 30 days)
mads skibsted
mads skibsted on 7 Mar 2024
Commented: Rik on 7 Mar 2024
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 Comment
Rik
Rik on 7 Mar 2024
You're already storing every iteration. What exactly is your problem?

Sign in to comment.

Answers (1)

Joe Vinciguerra
Joe Vinciguerra on 7 Mar 2024
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 Comment
Rik
Rik on 7 Mar 2024
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]}

Sign in to comment.

Categories

Find more on Stress and Strain in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!