How to change non zero value into 0?

9 ビュー (過去 30 日間)
vikash kumar
vikash kumar 2019 年 8 月 21 日
編集済み: Jan 2019 年 8 月 21 日
I want to manipulate n*n matrix such the last 10 non-zeros value in every column of matrix and change them into zero.Matrix is in the form of binary[0,1].
  1 件のコメント
madhan ravi
madhan ravi 2019 年 8 月 21 日
Can't you mention the version your using?

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

採用された回答

Andrei Bobrov
Andrei Bobrov 2019 年 8 月 21 日
Let A - your binary array (n x n)
m = 10;
B = cumsum(A);
out = A.*(B(end,:) - m >= B);
  1 件のコメント
Jan
Jan 2019 年 8 月 21 日
編集済み: Jan 2019 年 8 月 21 日
Or in modern Matlab versions:
B = cumsum(A, 1, 'reverse');
out = A .* (B > m);

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

その他の回答 (2 件)

John D'Errico
John D'Errico 2019 年 8 月 21 日
編集済み: John D'Errico 2019 年 8 月 21 日
You want to reset the last 10 non-zeros to zero, in each column. So, let me build an example matrix.
A = rand(20,5) > 0.25
A =
20×5 logical array
1 0 0 1 1
1 1 1 1 1
1 1 0 1 1
1 1 1 0 1
0 1 1 1 1
1 1 1 1 1
1 1 0 1 0
1 1 0 1 1
1 1 1 1 1
0 0 1 1 0
0 0 1 0 1
0 0 1 1 1
1 1 0 0 0
0 1 0 1 1
1 1 1 1 1
1 0 0 1 0
1 1 1 0 1
1 0 1 1 1
1 1 1 0 0
1 1 1 1 1
In this kind of boring example, there should be roughly 15 non-zeros in each column. But you want to kill off only the last 10 in each column.
The trivial way to do this is to use a loop. It is always a good idea to think of a SIMPLE way to solve a problem, and only then, if it is a problem, then worry about finding a better solution.
B = A;
for i = 1:size(A,2)
B(find(A(:,i),10,'last'),i) = 0;
end
We can verify that it worked, simply enough.
sum(A,1)
ans =
15 14 13 15 15
sum(B,1)
ans =
5 4 3 5 5
B
B =
20×5 logical array
1 0 0 1 1
1 1 1 1 1
1 1 0 1 1
1 1 1 0 1
0 1 1 1 1
1 0 0 1 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
It is clear the loop worked as expected. It is simple, and sufficient to solve the problem, and not even that inefficient. The code is concise, and easy to understand. It will run in almost any version of MATLAB since it only needs the 'last' option for find, which was introduced into MATLAB, and that must have been well over 20 years since that happened.
If there were issues with time (and ONLY then) I would then consider more sophisticated ideas. Do you have many thousands, or even millions of columns to work on? Will this code run millions of times? If not, then don't bother looking for something more efficient.

KSSV
KSSV 2019 年 8 月 21 日
If A is your matrix. To get non-zero indices use:
idx = A>0 ;
To replace them to zero use:
A(idx) = 0 ;
  9 件のコメント
KSSV
KSSV 2019 年 8 月 21 日
This one?
idx = A>0 ;
A(idx) = 0 ;
madhan ravi
madhan ravi 2019 年 8 月 21 日
KSSV no, if for instance , last two non zeros in each column to zero:
>> A
A =
4×2 logical array
0 0
1 1
0 1
1 1
>> Expected_result
Expected_result =
4×2 logical array
0 0
0 1
0 0
0 0
>>
Clear now?

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by