Short code to calculate and display value of matrix based on condition

조회 수: 4 (최근 30일)
Hi. I want to calculate new value in matrix C and based on conditions from value of matrix P. I want to display values cell string D in new cell string E based on chosen values of matrix P.
P =
1.0000 0.8181 0.9960 0.0000
0.8181 1.0000 0.5233 0.0000
0.9960 0.5233 1.0000 0.0000
0.0000 0.0000 0.0000 1.0000
D =
'A'
'B'
'C'
'D'
I want to calculate C and display E like this,
-1- if any value of P is less than 0.05, it will become 1, else it will become 0 in C.
-2- Then, sum values of C by each row. For example, row 1, 2, 3 will become 1, row 4 will become 3. After that, I would like to divide total of each row by number of all C columns, which is 4.
C =
0.25
0.25
0.25
0.75
-4- Display E cell string with value from D cell string by matching same column number of P values which is less than 0.05 with row number of D values. It will become like this.
E =
'D'
'D'
'D'
'A','B','C'

채택된 답변

KSSV
KSSV 2017년 7월 4일
First part can be achieved by this:
P = [ 1.0000 0.8181 0.9960 0.0000
0.8181 1.0000 0.5233 0.0000
0.9960 0.5233 1.0000 0.0000
0.0000 0.0000 0.0000 1.0000];
A = zeros(size(P)) ;
A(P<0.05) = 1 ;
second part is not clear.
  댓글 수: 2
yue ishida
yue ishida 2017년 7월 4일
I already edit my question. I hope to get feedback on my question.

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

추가 답변 (2개)

Image Analyst
Image Analyst 2017년 7월 4일
It looks like you've already accepted an answer. Here is an answer that works:
P = [...
1.0000 0.8181 0.9960 0.0000
0.8181 1.0000 0.5233 0.0000
0.9960 0.5233 1.0000 0.0000
0.0000 0.0000 0.0000 1.0000]
[rows, columns] = size(P);
D ={...
'A'
'B'
'C'
'D'}
% Step 1: compute C
C0 = P < 0.05
% Step 2: Divide C by the number of Rows:
C = sum(C0, 2) ./ columns
% Step 3:
% Display E cell string with value from
% D cell string by matching same column number of P values
% which is less than 0.05 with row number of D values.
for row = 1 : rows
indexes = find(C0(row, :))
E{row} = D(indexes)
end
If this one works, then can you Accept it, or "Vote" for it?

Image Analyst
Image Analyst 2017년 7월 4일
"if any element of P is less than 0.05, it will become 1, else it will become 0 in C. "
C = P < 0.05;
"that chosen P element will call value of E" <=== WHAT element of P. And how can an element call a value? A program or function can call a function, but an element cannot call a value.
"I want to sum total 1 by row," <== No idea what this means. "divide by total column P which is 4" <== Divide WHAT? And what is the "total column P"? Do you mean the sum of all elements in one or more columns of P? And how does any row or any columns of P sum to 4???
  댓글 수: 1
yue ishida
yue ishida 2017년 7월 4일
Thank you for your feedback. I will edit my question to become understandable.

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

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by