How do I insert data into new format based on row positions?

1 回表示 (過去 30 日間)
AV
AV 2021 年 1 月 7 日
コメント済み: AV 2021 年 1 月 8 日
Hi All - i'm having difficulty getting my data in the right format and wondered if you could help?
% Here is my data
data = [8; 7; 1; 5]
% I have the row positions of where the data sits in the desired format
datarowpositions = [0; 0; 0; 4; 0; 1; 0; 2; 0; 0; 3; 0]
% This is how I would like the data to be moved into
desired = [0; 0; 0; 5; 0; 8; 0; 7; 0; 0; 1; 0]
Thank you so much for your time.

採用された回答

Stephen23
Stephen23 2021 年 1 月 7 日
編集済み: Stephen23 2021 年 1 月 7 日
data = [8; 7; 1; 5];
datarowpositions = [0; 0; 0; 4; 0; 1; 0; 2; 0; 0; 3; 0];
desired = datarowpositions;
desired(datarowpositions~=0) = data(nonzeros(datarowpositions))
desired = 12×1
0 0 0 5 0 8 0 7 0 0
... more rows here

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by