-6

I have arrays at the same size:

a = 5:10;
b = [1 0 1 1 0 0];

I want to select the element where in the boolean array (b) is 1.

c = [5 7 8];

I want to do it in elegant way without loop.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
Tal
  • 1,145
  • 2
  • 14
  • 32
  • 3
    This is in MATLAB's language fundamentals: http://www.mathworks.com/help/matlab/math/matrix-indexing.html#bq7egb6-1. Not really sure why it's worth a Q&A – sco1 Aug 31 '16 at 14:33
  • 1
    @Tal, the question already has an upvoted answer, so it won't be deleted. Although asking duplicate questions should preferably be avoided, keeping them isn't only bad. People use different search terms on google, thus some might land on this question instead of the more thorough one. Please consider accepting CKT's answer =) http://meta.stackexchange.com/a/32315/243806 – Stewie Griffin Aug 31 '16 at 16:13
  • If you want people will keep good answer to duplicate quastion you need to reward them. – Tal Sep 01 '16 at 07:05
  • 1
    Don't deface your post. We *don't* want people to answer duplicate questions. We want askers to do their research before asking and find the duplicate target themselves; if they fail to do so, we have to resort to closing posts as duplicate. But regardless, if you ask a question and get an answer that solves it, it's most natural (and basic courtesy) to mark the answer as accepted. There is no obligation for you to do so, but refusing will make you seem like . – Andras Deak -- Слава Україні Sep 01 '16 at 08:04
  • If you keep my duplicate question, please reward it, other wise I don't want to keep her. don't force to people to get negative reward if they don't want to. It will spoil the community. – Tal Sep 02 '16 at 09:28
  • @Tal Once again, no. Don't deface the question. – Magisch Nov 30 '16 at 09:03
  • @Tal It is not allowed, you are not allowed to deface your question. Continuing to do so may result in the question being locked and/or you being suspended. – Magisch Nov 30 '16 at 09:05

1 Answers1

4

You can just do c = a(logical(b)) if b is not already logical. If it is, then just c = a(b).

CKT
  • 781
  • 5
  • 6