how should i define condition in for loop to get the index of a vector .?

1 ビュー (過去 30 日間)
Mustafa AL-Kalsh
Mustafa AL-Kalsh 2018 年 10 月 29 日
回答済み: jonas 2018 年 10 月 29 日
hello all, I have a Binary vector, and I want to check each element of this vector with the condition to apply some changes depend if 0 or 1 from this vector, I connect this vector with for loop still the condition to get the index how should I write it after entering the loop.
  1 件のコメント
madhan ravi
madhan ravi 2018 年 10 月 29 日
an example?

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

回答 (1 件)

jonas
jonas 2018 年 10 月 29 日
It sounds like you should learn about logical indexing instead of trying to solve your issue with loop.
For example, consider a binary vector id = [0 1 0 1], and an array, A = [1 2 3 4]. You want to take the sum of those elements in A where id = true?
sum(A(id))
ans =
6
Simple as that

Community Treasure Hunt

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

Start Hunting!

Translated by