Borrar filtros
Borrar filtros

I want to divide each row of a matrix with corresponding element in another matrix .Please help.

75 visualizaciones (últimos 30 días)
For eg: I have a matrix
A=[9 6 3;
4 2 8;
1 0 0;]
divide A by B=[3 2 1] to get the result
C=[3 2 1;
2 1 4;
1 0 0;]

Respuesta aceptada

Stephen23
Stephen23 el 18 de Abr. de 2017
Editada: Stephen23 el 18 de Abr. de 2017
Use bsxfun:
>> A = [9,6,3;4,2,8;1,0,0];
>> B = [3,2,1];
>> bsxfun(@rdivide,A,B(:))
ans =
3 2 1
2 1 4
1 0 0

Más respuestas (2)

Matt J
Matt J el 17 de Abr. de 2017
Editada: Matt J el 17 de Abr. de 2017
Assuming you have R2016b or higher,
C=A./B;
and if you do not,
C=bsxfun(@rdivide,A,B);
  1 comentario
SUNANNA S S
SUNANNA S S el 18 de Abr. de 2017
Sir, This gives the answer
C =
3.0000 3.0000 3.0000
1.3333 1.0000 8.0000
0.3333 0 0
I want to divide each row in A by the corresponding value in B that is, rownumber=element in B, if we take first row of A I want to divide it by first element in B and so on.

Iniciar sesión para comentar.


First Last
First Last el 28 de Mzo. de 2018
Editada: First Last el 28 de Mzo. de 2018
Make sure B is transposed to a column vector, i.e.,
C=A./B';

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