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

1 次查看(过去 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 个)

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???

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by