フィルターのクリア

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

2 ビュー (過去 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 Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by