How can i create N copies of a vector based on nonzero values in that vector?

조회 수: 3 (최근 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]

채택된 답변

Jan
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
Mohab Mostafa
Mohab Mostafa 2015년 11월 30일
편집: Mohab Mostafa 2015년 11월 30일
Great this is very good, what if i have a 64x1 vector and i want to have 4 nonzero in each copy (of course if there is a 27 nonzero values in the original vector, the last copy will contain only 3)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

William
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..
  댓글 수: 1
Mohab Mostafa
Mohab Mostafa 2015년 11월 30일
I was giving that vector as an example, i don't know the index of the nonzero values, and vector might be larger than that

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by