export specific information of a matrix into a new matrix in desired format

1 回表示 (過去 30 日間)
Homayoon
Homayoon 2015 年 6 月 22 日
コメント済み: Star Strider 2015 年 6 月 22 日
Dear All, I have a matrix in the following form:
1 0 0 1
2 1 0 0
3 0 0 0
4 0 0 0
5 1 0 0
the matrix is huge but has only four columns. What I basically need to have is a code to focus on the second column of the matrix and whenever the element on the second column is equal to 1, the associated row corresponding to that element will be reported in a different matrix. For the above example, the desired output must be:
2 1 0 0
5 1 0 0
I tried to write a code using [counts, bins]=hist, but it failed. I am so thankful of your kindness. All the best, HRJ

採用された回答

Star Strider
Star Strider 2015 年 6 月 22 日
This works:
A = [1 0 0 1
2 1 0 0
3 0 0 0
4 0 0 0
5 1 0 0];
B = A(A(:,2) == 1, :)
where ‘B’ is the output matrix.
  2 件のコメント
Homayoon
Homayoon 2015 年 6 月 22 日
of course, my mind was locked :D yeah, I do appreciate your help
Star Strider
Star Strider 2015 年 6 月 22 日
My pleasure!
I also know the feeling.

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by