How to delete zeros from a vector and place it again in that vector?

조회 수: 5 (최근 30일)
Deepti Ranjan Majhi
Deepti Ranjan Majhi 2017년 10월 8일
편집: dpb 2017년 10월 9일
I have a vector like,
A = [2000 -1000 0 0 0 2000 -1000 0 0 0 2000 -1000 0 0 0 1000]';
For removing zeros, I did,
B = A(A ~= 0);
Now, I want the same vector A from B. Is there any MATLAB functions available or I need to code it?
Thank you.
  댓글 수: 2
Cedric
Cedric 2017년 10월 8일
What do you mean by "same vector"? Do you want to re-expand B into A?

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

채택된 답변

dpb
dpb 2017년 10월 8일
편집: dpb 2017년 10월 8일
Once you've thrown away the zeros, they're gone -- there's no way of knowing where they were in the original A from B alone.
To do this you'll have to save the locations of the zeros in A (or non-zero, one is just the complement of the other) as well as total length in order to rebuild the vector from the pieces.
iNZ=find(A); % nonzero locations in A
lA=length(A); % total length
with those additional pieces of information and B, then
A=zeros(1,lA); A(iNZ)=B;
will reproduce A. But, again, w/o the additional info it's not possible to rebuild A precisely; too little information remains.
ADDENDUM
Actually, you can do it with only one additional variable; if you save the logical array instead of the locations only, then you have the length of the original array implicitly.
iNZ=(A~=0); % logical array instead of vector of locations
then, having B and iNZ
A(iNZ)=B;
will reconstruct A. Of course, since size(iNZ) == size(A), you might as well just save A to begin with. :)

추가 답변 (2개)

Image Analyst
Image Analyst 2017년 10월 8일
You can do it if you save the indexes you deleted. Try this
A = [2000 -1000 0 0 0 2000 -1000 0 0 0 2000 -1000 0 0 0 1000]'
nonZeroIndexes = A ~= 0
B = A(nonZeroIndexes)
% Get A back from B.
% If A is unavailable, you're going to at least have
% nonZeroIndexes still available or you can't do it.
k2 = 1;
A2 = zeros(length(nonZeroIndexes), 1); % Preallocate
for k = 1 : length(A2)
if nonZeroIndexes(k)
A2(k) = B(k2);
k2 = k2 + 1;
end
end
A2 % Print to command window.

dpb
dpb 2017년 10월 8일
"Is there any MATLAB functions available...?"
Actually, there is for the particular case of removing/recovering zeros --
>> B=sparse(A);
>> clear A
>> A=full(B)
A =
Columns 1 through 8
2000 -1000 0 0 0 2000 -1000 0
Columns 9 through 16
0 0 2000 -1000 0 0 0 1000
>>
  댓글 수: 2
Deepti Ranjan Majhi
Deepti Ranjan Majhi 2017년 10월 9일
Thank you very much @dpb. Your answers really save a lot of my time.
dpb
dpb 2017년 10월 9일
편집: dpb 2017년 10월 9일
Note, of course, that for this case B still "knows about" the zeros and they're not actually gone so that if you use it in an arithmetic expression you'll not get what you might expect/want. What is a better solution depends on what we don't know; the application for B
For example
>> mean(B)
ans =
(1,1) 250
>> mean(A)
ans =
250
>> mean(A(find(A)))
ans =
571.4286
>>
Also in the realm of "are there functions?" is
B=nonzeros(A);
as shorthand for A(find(A)) but again from it alone you can't reconstruct A; it's that requirement that's the kicker and if that's what's necessary unless you're running into other memory issues as noted in first response you might as well in all likelihood just keep the original to begin with as being more efficient overall use of resources.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by