フィルターのクリア

Count vector position in a matrix

1 回表示 (過去 30 日間)
steve Brian
steve Brian 2020 年 4 月 9 日
コメント済み: the cyclist 2020 年 4 月 10 日
Hi all. I need an algorithm, which takes my first element (! = 0) from an array and checks that element in a vector that already contains the elements of the matrix (! = 0) and begins to count the position it is in. Once it has found the respective element, it adds its position in another vector.
Ex: I have A = [ 2 3 4 0;
8 7 0 0;
2 0 1 3]
VectorA= [ 2 3 4 8 7 2 1 3] (elements != 0)
rowptr = [1 4 6]
Explain:
2 is in position 1 in the vectorA -> rowptr = [ 1
8 is in position 4 in the vectorA -> rowptr = [ 1 4
2 is in position 6 -> rowptr = [1 4 6]
A little help, please!

採用された回答

the cyclist
the cyclist 2020 年 4 月 9 日
Here's one way:
tmp = cumsum(sum(A~=0,2))
rowptr = [1; tmp(1:end-1)+1];
  2 件のコメント
steve Brian
steve Brian 2020 年 4 月 10 日
編集済み: steve Brian 2020 年 4 月 10 日
This didn't work
edit: I solved. Thanks bro!!
the cyclist
the cyclist 2020 年 4 月 10 日
Hm. I believe it worked on your example.
I'd be curious to see an example where this algorithm does not work, and your solution.
Maybe you should post your own code as a solution, and accept it, rather than accepting my incorrect solution.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by