Short code to calculate and display value of matrix based on condition
1 view (last 30 days)
Show older comments
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'
0 Comments
Accepted Answer
KSSV
on 4 Jul 2017
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.
More Answers (2)
Image Analyst
on 4 Jul 2017
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?
0 Comments
Image Analyst
on 4 Jul 2017
"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???
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!