How to skip over zero values in an array?

10 ビュー (過去 30 日間)
Peter Kapteyn
Peter Kapteyn 2017 年 11 月 15 日
コメント済み: Akira Agata 2017 年 11 月 16 日
In an array of audio samples, I would like to copy non-zero samples to a different array, and skip over any zero samples. I have tried using a loop to traverse the array, but I am having issues with bounds.
arrayout = [];
% arraycount = 1;
dim = size(dialsound); % number of samples in audio sample I am using
c = 1;
while (c < dim(1,2))
while(dialsound(1,c) == 0)
c = c + 1;
end
arrayout(1,c) = dialsound(1,c);
c = c + 1;
end

採用された回答

Akira Agata
Akira Agata 2017 年 11 月 15 日
You can also do that by another way, like:
idx = abs(dialsound) < eps;
arrayout = dialsound(~idx);
  2 件のコメント
Peter Kapteyn
Peter Kapteyn 2017 年 11 月 16 日
Any chance you could explain how this code works? Thanks.
Akira Agata
Akira Agata 2017 年 11 月 16 日
Thank you for your reply. To understand how this code works, I would recommend trying very simple example, like:
>> x = [1 2 0 2 1e-20]
x =
1.0000 2.0000 0 2.0000 0.0000
>> idx = x < eps
idx =
0 0 1 0 1
>> y = x(~idx)
y =
1 2 2
I hope it will be some help for your better understanding !

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

その他の回答 (1 件)

Matt J
Matt J 2017 年 11 月 15 日
Is this what you want
arrayout=nonzeros(dialsound)

カテゴリ

Help Center および File ExchangeAudio I/O and Waveform Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by