# Store values i Matrix from a loop

3 ビュー (過去 30 日間)
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 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Rik 2024 年 3 月 7 日

サインインしてコメントする。

### 回答 (1 件)

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;
Maxima{i,2} = i;
(or swap the index positions for a horizontal versus vertical array)
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
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 Center および File ExchangeGraphics Object Programming についてさらに検索

### Community Treasure Hunt

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

Start Hunting!

Translated by