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

2 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Jayant
Jayant il 29 Giu 2023
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 Commenti
Stephen Lesko
Stephen Lesko il 29 Giu 2023
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 il 29 Giu 2023
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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Stress and Strain in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by