non zero elements above 70 and below -70 zero

hello people,
In a matrix I want that all non zero elements above 70 and below -70 is zero.
How do I do that ?
Thank you!

Antworten (4)

Honglei Chen
Honglei Chen am 17 Feb. 2012

0 Stimmen

Use logical index
x(x>70 | x<-70)=0

2 Kommentare

Jeroen
Jeroen am 17 Feb. 2012
and what if I want to replace only those elements in the first two rows?
Honglei Chen
Honglei Chen am 17 Feb. 2012
See carmen's answer below

Melden Sie sich an, um zu kommentieren.

the cyclist
the cyclist am 17 Feb. 2012

0 Stimmen

I am not 100% sure what you mean, but here is some code (with comments on my understanding) that you should be able to modify to do what you want
% Some test data:
A = 200*rand(100) - 100;
% Index to values you want to zero out
idx = (A>70) | (A<-70); % Index to values that are bigger than 70 or smaller than -70
% Zero those out
A(idx) = 0;

1 Kommentar

Jeroen
Jeroen am 17 Feb. 2012
Thank you!
Well it is a matrix of ~40000 by 6. But what if I want to replace only those elements in the first two rows?

Melden Sie sich an, um zu kommentieren.

carmen
carmen am 17 Feb. 2012

0 Stimmen

then you modify Honglei Chen 's approach according to your wish:
x(1:2,x(1:2,:)>70 | x(1:2,:)<-70)=0
that should be it.

Tags

Gefragt:

am 17 Feb. 2012

Bearbeitet:

Jan
am 15 Okt. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by