Update matrix using vector of column index
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Georgio Mazarin
el 25 de Mzo. de 2017
Comentada: Georgio Mazarin
el 25 de Mzo. de 2017
I have a 2-dim matrix like this:
0.3436 0.3283 0.3890 0.0274
0.1137 0.4443 0.3484 0.0889
0.2748 0.3124 0.4074 0.0439
and a vector with column indexes for each row
3
2
2
Task is to invert values to get result like this:
0.3436 0.3283 -0.3890 0.0274
0.1137 -0.4443 0.3484 0.0889
0.2748 -0.3124 0.4074 0.0439
I believe there is a simple way to do that efficiently without for-loop cycles. Any idea?
0 comentarios
Respuesta aceptada
the cyclist
el 25 de Mzo. de 2017
Editada: the cyclist
el 25 de Mzo. de 2017
M = [0.3436 0.3283 0.3890 0.0274
0.1137 0.4443 0.3484 0.0889
0.2748 0.3124 0.4074 0.0439];
v = [3 2 2];
[m,n] = size(M);
linearIndex = sub2ind([m n],1:m,v);
M(linearIndex) = -M(linearIndex)
Note that the 2nd and 3rd arguments to sub2ind have to be the same shape, so make sure they are both column vectors, or both row vectors, but not mixed.
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrices and Arrays 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!