Extract max value and corresponding index from a cell into seperate matrixes in a for loop

1 次查看(过去 30 天)
Hi, I want to extract the index and max value from a cell containing vectors. Currently when I run this code it saves the max stress and index as just a variable rather than a matrix of each vectors max and index.
peak_stress = [];
stress = [];
index = [];
for p=1:numel(sigma)
peak_stress = [peak_stress, max(sigma{1,p}(1:end))]; %saves max stress as a vector (works fine)
[stress,index] = max(sigma{1,p}(1:end)); %goal: create a matrix for stress and a matrix for index from sigma cell sigma{1xn}(n,1)
end

采纳的回答

Jayant
Jayant 2023-6-29
What I understood from you question is that you want to store the max stress and index.
These values can be appended to the stress and index matrices using the concatenation operator (;).
for p=1:numel(sigma)
peak_stress = [peak_stress, max(sigma{1,p}(1:end))]; %saves max stress as a vector (works fine)
[stress,index] = max(sigma{1,p}(1:end)); %goal: create a matrix for stress and a matrix for index from sigma cell sigma{1xn}(n,1)
% Append the values to the matrices
stress = [stress; max_stress];
index = [index; max_index];
end
After the loop, the stress matrix will contain the maximum stress values from each vector, and the index matrix will contain the corresponding indices.
I hope this helps.
  2 个评论
Stephen Lesko
Stephen Lesko 2023-6-29
This idea makes sense, but when I tried implementing the code it the stress and index are still being saved as values rather than a matrix.
Stephen Lesko
Stephen Lesko 2023-6-29
It works with a slight naming change to loop.
for p=1:numel(sigma)
peak_stress = [peak_stress, max(sigma{1,p}(1:end))]; %saves max stress as a vector (works fine)
[max_stress,max_index] = max(sigma{1,p}(1:end)); %goal: create a matrix for stress and a matrix for index from sigma cell where sigma{1xn}(n,1)
% Append the values to the matrices
stress = [stress, max_stress];
index = [index, max_index];
end
Thank you for the help.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Stress and Strain 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by