フィルターのクリア

converting a double array to a logical array conditionally

1 回表示 (過去 30 日間)
Matt Talebi
Matt Talebi 2015 年 6 月 16 日
コメント済み: Matt Talebi 2015 年 6 月 16 日
Double array A is [2 1 6 7 3 5 4]. How can I convert it to a logical array in a way to get 1 for the first (for example) 3 elements and 0 for the rest. i.e., B = [1 1 0 0 0 1 0]. I apologize for the very basic nature of this question but unfortunately couldn't find any answer for it!
  2 件のコメント
Matt Talebi
Matt Talebi 2015 年 6 月 16 日
2, 1, and 6 are the first 3 elements of the double array A and I want only them to be 1 in the resulting logical array B (and zero for the rest).
Matt Talebi
Matt Talebi 2015 年 6 月 16 日
Thanks Per but unfortunately your codes returns: [1 1 1 0 0 0 0]; What I want is [1 1 0 0 0 1 0]! Let me explain my problem in this way: I have a 1*800 double array in no particular order (A=[21,46,742,16,650, ...]). I'm just interested in the first let's say 3 elements of this array, which are 21, 46 and 742. What I want is a logical array with the same length BEING IN ASCENDING ORDER FROM 1 TO 800 returning 1 for the 21th, 46th and 742nd elements and zero for the rest. Hope you can help with this.

サインインしてコメントする。

採用された回答

Andrei Bobrov
Andrei Bobrov 2015 年 6 月 16 日
編集済み: Andrei Bobrov 2015 年 6 月 16 日
A = [2 1 6 7 3 5 4];
B = ismember(sort(A),A(1:3));

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 6 月 16 日

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by