Borrar filtros
Borrar filtros

replace the elements of a matrix

1 visualización (últimos 30 días)
pavlos
pavlos el 13 de Feb. de 2014
Comentada: Dishant Arora el 13 de Feb. de 2014
Hello,
Consider a 100x1 matrix, called A, that contains random values of "0" and "1".
The number of "1" is 25 and the rest 75 are "0".
Also, consider a matrix 25x1, called B, that contains random values.
How can I replace the "1" of matrix A with the values of matrix B?
The "0" of A would not be influenced.
Thank you.
Best,
Pavlos

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 13 de Feb. de 2014
  2 comentarios
pavlos
pavlos el 13 de Feb. de 2014
Thank you for your response.
Unfortunately, the above code do not work.
It works only when A and B have the same dimensions.
In my case, A is 100x1 and B is 25x1.
The number of "1" in A are 25.
Azzi Abdelmalek
Azzi Abdelmalek el 13 de Feb. de 2014
This is not true. Look at this example
%exmple
A=zeros(100,1);
A(randperm(100,25))=1
B=rand(25,1)
%----------------------
A(A==1)=B

Iniciar sesión para comentar.

Más respuestas (1)

Dishant Arora
Dishant Arora el 13 de Feb. de 2014
A(find(A==1))=B;
  2 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 13 de Feb. de 2014
Why to add find when you can do it without it?
Dishant Arora
Dishant Arora el 13 de Feb. de 2014
yeah,got it

Iniciar sesión para comentar.

Categorías

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

Community Treasure Hunt

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

Start Hunting!

Translated by