How to remove zeroes not defined by indices

2 ビュー (過去 30 日間)
Suraj
Suraj 2020 年 11 月 5 日
編集済み: Suraj 2020 年 11 月 6 日
Hello All,
I have a large signal data file (about 3000000x1 double) and I first ran these values under a defined signal range and replaced the non conforming values to zero.
However, some of the values of the original data also contain zeroes for which I have the corresponding indices.
  1. How do I remove the other zeroes from the data but keep the zeroes corresponding to the indices?
Eg: 1. A = [ 1 2 0 0 3 0 4 0 5 6 0 9] -> Data
B = [3,6,8] -> Indices corresponding to the zeroes in A
Expected Output: [1 2 0 3 0 4 0 5 6 9]
Below is a code snippet for this logic that I tried but without success.
%Thr = Measured Data Signal (Appox. 3000000 x 1 double)
%idx = Indices for zeros to be retained in data signal
Thr2 = zeros(size(Thr));
Thr3 = Thr;
for k = 1:numel(idx)
Thr2(idx(k)) = idx(k);
end
for k1 = 1:numel(Thr)
if((Thr(k1)== 0)&&(k ~= Thr2(k)))
Thr3(k1) = [];
else
Thr3(k1) = Thr3(k1);
end
end
Any help would be appreciated.
Cheers!
  2 件のコメント
madhan ravi
madhan ravi 2020 年 11 月 5 日
By the way your expect output should be
[1
2
0
3
4
0
5
0
6
9]
Suraj
Suraj 2020 年 11 月 5 日
編集済み: Suraj 2020 年 11 月 6 日
Hello Madhan,
In the example, B corresponds to the indices of the values that are equal to zero and that which need to be retained.
Therefore, only the zeroes that are present in the index values 3,6,8 need to be retained and the other zeroes (In indices 4, 11] need to be removed.
Thanks!

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

採用された回答

madhan ravi
madhan ravi 2020 年 11 月 5 日
編集済み: madhan ravi 2020 年 11 月 5 日
Wanted = zeros(numel(nonzeros(A)) + numel(B), 1);
Wanted(setdiff(1 : numel(Wanted), B)) = nonzeros(A)
  3 件のコメント
madhan ravi
madhan ravi 2020 年 11 月 6 日
編集済み: madhan ravi 2020 年 11 月 6 日
My two options give you the desired answer, take the time and respond.
output = A;
output(B) = nan;
output = nonzeros(output);
output(isnan(output)) = 0
Suraj
Suraj 2020 年 11 月 6 日
Perfect! Thanks for your help Madhan!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultirate Signal Processing についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by