MATLAB Answers

What is the most efficient way to create a logical index from values in a matrix?

2 views (last 30 days)
Pati Stan
Pati Stan on 15 Dec 2018
Answered: Star Strider on 15 Dec 2018
I have a vector of values that I want to be 1s in a logical vector of a given size. Is there any way to do this in one line of code?
Here is how I am currently doing it:
%given information
given_size = 15;
chosen_values = [2 4 5 10];
%get index - can this be done in one line?
chosen_cells_index = false(1,given_size);
chosen_cells_index(chosen_values) = 1;

  0 Comments

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 15 Dec 2018
I use three lines here for the complete illustration. If you have already assigned the first two lines, the third is all that is necessary.
Example —
v = randi(15, 1, 20);
chosen_values = [2 4 5 10];
chosen_cells_index = ismember(v, chosen_values)
The ‘chosen_cells_index’ vector will be what you want.

  0 Comments

Sign in to comment.

More Answers (0)