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

3 ビュー (過去 30 日間)
yue ishida
yue ishida 2017 年 7 月 4 日
回答済み: Image Analyst 2017 年 7 月 4 日
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 will edit back. Thanks
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 Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by