Short code to calculate and display value of matrix based on condition
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
yue ishida
el 4 de Jul. de 2017
Respondida: Image Analyst
el 4 de Jul. de 2017
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 comentarios
Respuesta aceptada
KSSV
el 4 de Jul. de 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.
2 comentarios
Más respuestas (2)
Image Analyst
el 4 de Jul. de 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 comentarios
Image Analyst
el 4 de Jul. de 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???
Ver también
Categorías
Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!