Converting a Data Array into a Larger Array Given a Logical Array

I'm attempting to apply a small data set onto a larger array given a logical array and can't figure out how to tackle the problem. As an example given a much smaller array.
Logical Array [1,0,1,0,1,0,0,0,1]
Data Array [5,3,7,4]
Output [5,0,3,0,7,0,0,0,4]
Any guidance would be apprieciated.

 採用された回答

Stephen23
Stephen23 2020 年 10 月 7 日
編集済み: Stephen23 2020 年 10 月 7 日

0 投票

>> idx = logical([1,0,1,0,1,0,0,0,1]);
>> dat = [5,3,7,4];
>> out = zeros(size(idx)); % preallocate
>> out(idx) = dat
out =
5 0 3 0 7 0 0 0 4

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

質問済み:

2020 年 10 月 7 日

編集済み:

2020 年 10 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by