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?
Deepti Ranjan Majhi
Deepti Ranjan Majhi 2017 年 10 月 8 日
Yes Cedric.

サインインしてコメントする。

採用された回答

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.
  1 件のコメント
Deepti Ranjan Majhi
Deepti Ranjan Majhi 2017 年 10 月 8 日
Thank you for the answer Image Analyst. Your code is perfectly fine.

サインインしてコメントする。


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 Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by