フィルターのクリア

Reducing nonzero values in vector and adding to subsequent zero values.

2 ビュー (過去 30 日間)
virkuz000
virkuz000 2016 年 2 月 16 日
コメント済み: virkuz000 2016 年 2 月 16 日
I have a large vector of mostly zeros. I wish to divide the nonzero values by a value of N, and then add the divided value to the subsequent N-1 zero values. For example, if my vector is [....0 0 15 0 0 0 0 0 0 0....] and N=5, I want it to come out [....0 0 3 3 3 3 3 0 0 0....]. Thank-you for any tips!

採用された回答

Image Analyst
Image Analyst 2016 年 2 月 16 日
編集済み: Image Analyst 2016 年 2 月 16 日
This works:
vector = [0 0 15 0 0 0 0 0 0 0 4 0 0 0 0 0 0 9 0]
N=5
nonZeroIndexes = find(vector ~= 0)
for k = 1 : length(nonZeroIndexes);
index1 = nonZeroIndexes(k)
index2 = min(index1 + N - 1, length(vector))
vector(index1 : index2) = vector(index1)/N;
end
% Display
vector
For max robustness, you'll have to define what you want to do in the case where two non-zero numbers are separated by less than N elements. It's not clear how you want to handle that.
  1 件のコメント
virkuz000
virkuz000 2016 年 2 月 16 日
Thanks! This does work in my program. And you're right, I will have to address when two nonzeros are less than N elements apart. Fortunately, I am helped by the fact that in the original vector, there are only 100 nonzero elements in a vector roughly 6000000x1 in size. So the likelihood of this problem occurring is very small.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by