Find indices in 2D Matrix

23 次查看(过去 30 天)
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);

采纳的回答

the cyclist
the cyclist 2021-7-29
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 个评论
Bianka Markovic
Bianka Markovic 2021-7-30
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 2021-7-30
编辑:the cyclist 2021-7-30
Ah, sorry. You must have an older version of MATLAB. Newer versions (since R2016b) have implicit expansion. Glad you found bsxfun.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by