How to delete zeros from a vector and place it again in that vector?
13 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposta accettata
dpb
il 8 Ott 2017
Modificato: dpb
il 8 Ott 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. :)
Più risposte (2)
Image Analyst
il 8 Ott 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.
dpb
il 8 Ott 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 Commenti
dpb
il 9 Ott 2017
Modificato: dpb
il 9 Ott 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.
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!