Faster than find on all the colomns

2 ビュー (過去 30 日間)
Jose Santos
Jose Santos 2019 年 3 月 1 日
コメント済み: Jose Santos 2019 年 3 月 1 日
I'm tryting to find a faster way than the one I already have. So I have a big matrix, with between 4 and 16 lines and around 5K colomns, which is sparcely populated with ones. What I need to do is: The first nonzero element of each colomn is too remain one, and if there are more non-zero elements they have to be multiplied by a constant coefficient.
The way I do it now is as follows: multiply all the elements by the coefficient, and then run throught the colomns (with for ), looking for the first element to turn it back to one, with find. Like on the example below. I'm just thinking if I could not do it without having to go through all the colomns looking for the first nonzero element, as the code have to be run several times on the simulation.
a= [1] [0]
[0] [1]
[1] [0]
[0] [0]
then
a= [K] [0]
[0] [K]
[K] [0]
[0] [0]
then (with for + find)
a= [1] [0]
[0] [1]
[K] [0]
[0] [0]

採用された回答

Andrei Bobrov
Andrei Bobrov 2019 年 3 月 1 日
a = rand(5,15)>.7;
K = 100;
lo = cumsum(a) > 1;
a(lo) = a(lo)*K;
  1 件のコメント
Jose Santos
Jose Santos 2019 年 3 月 1 日
Great thanks, the example does not work with "a", as it is a logical matrix, but with my example it does work. Thanks

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by