gintas
gintas

Reputation: 2178

Select checked items from an array in Matlab?

I have two arrays. One for data and one which contains 1 for each item which I want to select from an array and 0 for each item which I want to ignore.

data = [1 2 3 4 5];
list = [1 0 1 0 1];

Is there a quick one-liner way to get checked elements (1, 3 and 5) from the data array without doing something like:

newdata = [];
for i=1:numel(data)
    if list(i) == 1
        newdata(end+1) = data(i);
    end
end        

Upvotes: 0

Views: 74

Answers (1)

YXD
YXD

Reputation: 32511

You can use it directly:

data(list == 1)

or

data(logical(list))

Upvotes: 4

Related Questions