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

4 vues (au cours des 30 derniers jours)
Deepti Ranjan Majhi
Deepti Ranjan Majhi le 8 Oct 2017
Modifié(e) : dpb le 9 Oct 2017
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 commentaires
Cedric
Cedric le 8 Oct 2017
What do you mean by "same vector"? Do you want to re-expand B into A?

Connectez-vous pour commenter.

Réponse acceptée

dpb
dpb le 8 Oct 2017
Modifié(e) : dpb le 8 Oct 2017
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. :)

Plus de réponses (2)

Image Analyst
Image Analyst le 8 Oct 2017
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.
  1 commentaire
Deepti Ranjan Majhi
Deepti Ranjan Majhi le 8 Oct 2017
Thank you for the answer Image Analyst. Your code is perfectly fine.

Connectez-vous pour commenter.


dpb
dpb le 8 Oct 2017
"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 commentaires
Deepti Ranjan Majhi
Deepti Ranjan Majhi le 9 Oct 2017
Thank you very much @dpb. Your answers really save a lot of my time.
dpb
dpb le 9 Oct 2017
Modifié(e) : dpb le 9 Oct 2017
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by