필터 지우기
필터 지우기

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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by