how to build a new matrix with reference to the index of another matrix ?

5 visualizaciones (últimos 30 días)
I have a matrix cost(i,j)= cost(3,5)=
[ 0 0 -1.5572 0.2352 0
0 -3.2616 0 0 5.1371
-2.8496 0 0 0 0 ]
and I want to have MM(i,j) that the same of the above, but for each non-negative value will have zero . Thus, MM should be
[ 0 0 -1.5572 0 0
0 -3.2616 0 0 0
-2.8496 0 0 0 0 ]
Also, I want to have a RR(i,j) matrix that has one for each negative value. Thus, RR will be
[ 0 0 1 0 0
0 1 0 0 0
1 0 0 0 0 ]
Thanks a lot, extreme thanks..

Respuesta aceptada

Friedrich
Friedrich el 27 de Feb. de 2014
Hi,
try this
A = [ 0 0 -1.5572 0.2352 0
0 -3.2616 0 0 5.1371
-2.8496 0 0 0 0 ]
MM = A;
MM(A>0) = 0
RR = MM;
RR(MM<0) = 1

Más respuestas (1)

Jos (10584)
Jos (10584) el 27 de Feb. de 2014
MM = min(cost,0)
RR = cost<0
btw, the notation cost(i,j) means the element in the i-th row and j-th column of the array cost. I think you intend to say that cost has i rows and j columns. In that case, the following is more clear: " I have an i-by-j array cost "

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by