フィルターのクリア

Vectorized method to filling in array based on logical array

10 ビュー (過去 30 日間)
Noahyt
Noahyt 2016 年 8 月 4 日
コメント済み: Noahyt 2016 年 8 月 4 日
I am trying to fill in a new array using values that I have stored in an input vector (sort of like reformat, but more general).
I have a logical array that has true values at locations that correspond to where I want to place one of my input vector values. ONce I have used a value, I want to discard it and use the next value in the vector for the next "open" spot.
I realize that might not have been totally clear, so for example:
I have the input vector and input array
in_vec = [1,2,3,4,5];
in_arr = [0,0,0,0,0; 0,1,1,1,0; 0,0,1,1,0]
and I would like to get the output
out_arr = [0,0,0,0,0; 0,1,2,3,0; 0,0,4,5,0]
I know that I can do this using a for loop that iterates over every item in the logical array and inputting items in the target array, I'm wondering whether there is a smarter way to do this using a vectorized (or other) method?

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2016 年 8 月 4 日
編集済み: Azzi Abdelmalek 2016 年 8 月 4 日
Edit
in_vec = [1,2,3,4,5]
in_arr = [0,0,0,0,0; 0,1,1,1,0; 0,0,1,1,0]
out_arr =zeros(size(in_arr'))
out_arr(logical(in_arr'))=in_vec
out_arr=out_arr'
  1 件のコメント
Noahyt
Noahyt 2016 年 8 月 4 日
Thanks! I wouldn't have thought of that.

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by