Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Jayant
Jayant el 29 de Jun. de 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 comentarios
Stephen Lesko
Stephen Lesko el 29 de Jun. de 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 el 29 de Jun. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by