How can i create N copies of a vector based on nonzero values in that vector?
1 回表示 (過去 30 日間)
古いコメントを表示
I have a vector 1x5 which contain NZ nonzero values, what can i do if i want to create N copies of that vector such that each copy contains only 1 nonzero value from NZ?
Original Vector [1 0 5 0 3]
Copy 1 [1 0 0 0 0] Copy 2 [0 0 5 0 0] Copy 3 [0 0 0 0 3]
0 件のコメント
採用された回答
Jan
2015 年 11 月 30 日
v = [1 0 5 0 3];
Elem = unique(v(v~=0));
nElem = numel(Elem);
Result = cell(1, nElem);
for k = 1:nElem
Result{k} = v .* (v == Elem(k));
end
その他の回答 (1 件)
William
2015 年 11 月 30 日
not elegant but:
Original_Vector=[1 0 5 0 3];
Copy_1=Original_Vector;
Copy_1(3)=0;
Copy_1(5)=0;
etc..
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!