フィルターのクリア

How to assign value in an array depending on the index stated in another array

92 ビュー (過去 30 日間)
want2know
want2know 2014 年 2 月 17 日
コメント済み: Image Analyst 2014 年 2 月 17 日
Hi all,
After performing some processing, now the variable 'a' contains [2 5 9].
Let say i have another variable 'b', variable 'b' is pre-initialized to be zero depending on a length of input, let say 10. b = [0 0 0 0 0 0 0 0 0 0];
What I want is, the elements that are stated in variable 'a' should be set ONE in variable 'b'. b= [0 1 0 0 1 0 0 0 1 0]
How could I do this in Matlab? Appreciate any idea, thanks

採用された回答

Mischa Kim
Mischa Kim 2014 年 2 月 17 日
編集済み: Mischa Kim 2014 年 2 月 17 日
  1 件のコメント
want2know
want2know 2014 年 2 月 17 日
編集済み: want2know 2014 年 2 月 17 日
Thanks so much... What is wrong if I got this error when using b(a)=1?
I did make sure variable 'a' contains only positive integers.
Subscript indices must either be real positive integers or logicals.

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

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2014 年 2 月 17 日
Welcome to the world of indexing!
Your problem:
a = [2 5 9]
b = [0 0 0 0 0 0 0 0 0 0];
b(a) = 1
Other interesting indexing things:
a = [1 3 7]
b = [0 0 0 ];
b(a) = 1 % automatic expansion of b
a = [2 2 2 2]
b = [0 0 0 0]
b(a) = 1 % no adding up ...
  4 件のコメント
want2know
want2know 2014 年 2 月 17 日
Thanks. I did make sure variable 'a' contains only positive integers.
'a' can only be positive integers, I confirm and have checked.
I have also added this line "a=round(a); b(a)=1;" still I got the same error...
Image Analyst
Image Analyst 2014 年 2 月 17 日
What does all this show
a
class(a)
whos a
min(a(:))
max(a(:))

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

カテゴリ

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