简体   繁体   中英

Logical elements substitution [3D Matrix]

Let's say that I have 2 3D-Matrix:

A = rand(10,4,100);
B = rand(10,4,100);
L = gt(A,B);

Now I want substitute all elements of B with elements of A only where L==1 but this doesn't work:

B(L==1,:,:) = A(L==1,:,:);

Any suggestion?

我们甚至更短地发现

B(L) = A(L);

Sounds like a job for the find() function.

p = find(L);
B(p) = A(p);

EDIT: Just realized you don't need the find() function. Just use logical indexing like this:

B(L==1) = A(L==1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM