Extend / replicate a value by column when found in array

10 ビュー (過去 30 日間)
cedric W
cedric W 2018 年 9 月 6 日
編集済み: Guillaume 2018 年 9 月 6 日
Say I have a matrix 5x5 with binary entries 0 and 1
A=[1 1 1 1 1;1 0 1 1 1;1 0 1 0 0;0 1 1 0 1;1 1 1 0 0]
I'd like to have as a result, a matrix where when the array is parsed by column, if 0 is found, then all rows indexed after are 0:
B=[1 1 1 1 1;1 0 1 1 1;1 0 1 0 0;0 0 1 0 0;0 0 1 0 0]
Any clue for a fast computing solution ? FYI every column is independent.
  2 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2018 年 9 月 6 日
編集済み: KALYAN ACHARJYA 2018 年 9 月 6 日
@cedric The question is not clear for me
cedric W
cedric W 2018 年 9 月 6 日
That's why I tried to give an example: A is the input matrix, B is the output. Say columns are #of simulations, rows are days. If on a column,on one day the value is 0, then for all the next days it will remain at 0. And you loop for the number so simulations you have. Simulations (columns therefore) are independent. Hope this is clearer.

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

採用された回答

Guillaume
Guillaume 2018 年 9 月 6 日
編集済み: Guillaume 2018 年 9 月 6 日
This is trivially achieved with cumprod since as soon as a 0 is encountered in a column the cumulative product is 0 from then on:
A = [1 1 1 1 1;1 0 1 1 1;1 0 1 0 0;0 1 1 0 1;1 1 1 0 0]
B = cumprod(A)
  1 件のコメント
cedric W
cedric W 2018 年 9 月 6 日
Nice indeed. I was thinking about using cumsum but could find the solution. Thank you !

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by