Assign a label to a point of a matrix in if statement

조회 수: 2 (최근 30일)
MarshallSc
MarshallSc 2021년 6월 1일
편집: Stephen23 2021년 6월 2일
Hi, I'm trying to assign a label (based on a category from a table) to each element of a 10*10 matrix in an if statement such that if the value is smaller or larger from a threshold, the corrosponding element gets assigned a certain category label with the index of the element so that it can be known which element it is . For example:
Kappa=rand(10,10);
KappaLong=rand(10,10);
KappaShort=rand(10,10);
for i=1:10
for j=1:10
if ((abs(Kappa(i,j)))/(abs(KappaLong(i,j)))<1) && ((abs(Kappa(i,j)))/(abs(KappaShort(i,j)))<1)
Kappa(i,j)='Point is stable: Category 1 is assigned' %to show which element of the matrix is stable
elseif ((abs(Kappa(i,j)))/(abs(KappaLong(i,j)))>1) && ((abs(Kappa(i,j)))/(abs(KappaShort(i,j)))>1)
Kappa(i,j)='Point is unstable'
% (the code should continue after the elseif)
if abs(Kappalong)>abs(KappaShort)
ModKappa=abs(KappaLong)
else ModKappa=abs(KappaShort)
end
end
end
end
I know the code above is not complete or correct, just wanted to provide an example about what I mean. Thank you in advance.
  댓글 수: 3
MarshallSc
MarshallSc 2021년 6월 1일
Thanks for your reply Bob. Well, when I want to execute the code, I'll receive the following error in the line Kappa_j(i,j)='Point is stable: Category 1 is assigned' :
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-39.
I was wondering whether I'm writing this part incorrectly.
Stephen23
Stephen23 2021년 6월 2일
편집: Stephen23 2021년 6월 2일
Note that storing lots of scalar numeric data in a cell array makes processing that data much slower and more complex. You should probably store the "status" string in a separate string array, and keep the numeric data in a numeric array.

댓글을 달려면 로그인하십시오.

채택된 답변

the cyclist
the cyclist 2021년 6월 1일
By default, Kappa_j is going to be a numeric array, and there cannot accept character input. Instead, you could define it as a cell array:
Kappa_j = cell(2,2); % Preallocate the cell array, but smaller to illustrate
Kappa_j{1,1} = 'Point is stable: Category 1 is assigned'
Kappa_j = 2×2 cell array
{'Point is stable: Category 1 is assigned'} {0×0 double} {0×0 double } {0×0 double}
Notice the use of curly brackets to access the "inside" of the cell.
  댓글 수: 1
MarshallSc
MarshallSc 2021년 6월 1일
Thanks a lot sir, I was thinking that about the same method too. Thanks again.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by