question about indexing of logic matrix
Show older comments
Hi:
I read through the blog presented in link:
there is an operation using command:
mask(hel.faces)
Where hel.faces is a m*3 matrix, and mask is a n*1 logic matrix. I could not understand how could this operation happens because it can not reproduce in my side. my test commands are:
A=rand(20,3);
B=rand(20,1);
C=B>0.5;
C(A)
could anyone give me some suggestions?
Thanks!
Yu
Accepted Answer
More Answers (1)
Steven Lord
on 2 Oct 2019
This is linear indexing into a logical array.
A = [true false]
B = randi(2, 5, 5)
C = A(B)
The elements of C that are true (A(1)) correspond to elements in B that are 1.
The elements of C that are false (A(2)) correspond to elements in B that are 2.
Perhaps a different example, one that has a few more unique values from which to select, would illustrate the technique more clearly.
D = ["Ace of spades"; "Queen of hearts"; "7 of diamonds"; "Jack of clubs"]
selection = randi(numel(D), [4 4])
selectedCards = D(selection)
Element 11 of selectedCards is the element in D whose index is stored in element 11 of selection.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!