Quentin Pradet
Quentin Pradet

Reputation: 4771

Converting a "boolean" string to an array in MATLAB

I need to convert a string of boolean indices into an array which would look like this:

convert('11001') = [1 2 5]
convert('0000') = []
convert('001') = [3]

I don't control the function which produces the string.

Any ideas to do this in an elegant way? I already did it whith a loop but it looks wrong somehow.

Upvotes: 0

Views: 1458

Answers (2)

Richie Cotton
Richie Cotton

Reputation: 121127

Here's a variation that converts each character to a number.

function y = convert(s)
y = find(str2num(s')')

Upvotes: 2

Nzbuu
Nzbuu

Reputation: 5251

function y = convert(s)
y = find(s == '1');

Upvotes: 5

Related Questions