Hello everyone,
I'm trying to solve he problem with my indices. I have a 2D matrix A with ones and zeros. Now i want to define my other matrix B on those coordinates where A=1. But in the end i still want to have an 2D Matrix, not a vector with indeces.
Does anyone know how this could be solved.
Thank you!
A = rand(50,20)>.3;
B=rand(50,20);
newmatrix=B(A);

 Respuesta aceptada

the cyclist
the cyclist el 29 de Jul. de 2021
Is this what you mean? (I made the matrices smaller, just to show the result.)
A = rand(5,2)>.3;
B = rand(5,2);
newmatrix = zeros(size(A));
newmatrix(A) = B(A)
newmatrix = 5×2
0.5006 0 0.1912 0.2603 0.9736 0.3731 0 0.7064 0 0

5 comentarios

Bianka Markovic
Bianka Markovic el 29 de Jul. de 2021
Thank you for the answer! Really appreciate it.
Yes, this helps a lot thank you!
Bianka Markovic
Bianka Markovic el 29 de Jul. de 2021
Oh, I have one more question. If I have now a second matrix, which is for example C=(5,2,6) how can I use the idices from before for the two dimensions and take all other values from the third dimension?
the cyclist
the cyclist el 29 de Jul. de 2021
You can do
newC = C .* A;
which made me realize that the much easier way to do your original problem is
newmatrix = B .* A;
Bianka Markovic
Bianka Markovic el 30 de Jul. de 2021
Hm, it says that the matrix dimension must agree. But I made it with D = bsxfun(@times,C,B); :) Thanks again!
the cyclist
the cyclist el 30 de Jul. de 2021
Editada: the cyclist el 30 de Jul. de 2021
Ah, sorry. You must have an older version of MATLAB. Newer versions (since R2016b) have implicit expansion. Glad you found bsxfun.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2019b

Preguntada:

el 29 de Jul. de 2021

Editada:

el 30 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by