Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
What is wrong with this if statement

Subject: What is wrong with this if statement

From: anoop Sivasankaran

Date: 27 Aug, 2009 08:35:18

Message: 1 of 3

I am making a very fundamental mistake here, which I am not able to identify. I have a data file dat(320000X4)

r1 = dat(:,1);
r2 = dat(:,2);
a=0.86;
b=0.85;
c=0.87;
d=0.89;
k= find(dat(:,3) ~= 0);
if (r2(k) < b & r1(k) > a)
       plot(dat(k,1),dat(k,2),'g.');
  end

Why this is giving me an empty plot all the time?

Subject: What is wrong with this if statement

From: tpl@eng.cam.ac.uk (Tim Love)

Date: 27 Aug, 2009 08:44:57

Message: 2 of 3

"anoop Sivasankaran" <anooppgd@gmail.com> writes:

>I am making a very fundamental mistake here, which I am not able to identify. I have a data file dat(320000X4)

>r1 = dat(:,1);
>r2 = dat(:,2);
>a=0.86;
>b=0.85;
>c=0.87;
>d=0.89;
>k= find(dat(:,3) ~= 0);
>if (r2(k) < b & r1(k) > a)
> plot(dat(k,1),dat(k,2),'g.');
> end

>Why this is giving me an empty plot all the time?
What are you hoping that
   if (r2(k) < b & r1(k) > a)
does? Provide a minimal, complete example (with data) that replicates the problem.
 

Subject: What is wrong with this if statement

From: Wayne King

Date: 27 Aug, 2009 12:34:19

Message: 3 of 3

"anoop Sivasankaran" <anooppgd@gmail.com> wrote in message <h75gg6$odo$1@fred.mathworks.com>...
> I am making a very fundamental mistake here, which I am not able to identify. I have a data file dat(320000X4)
>
> r1 = dat(:,1);
> r2 = dat(:,2);
> a=0.86;
> b=0.85;
> c=0.87;
> d=0.89;
> k= find(dat(:,3) ~= 0);
> if (r2(k) < b & r1(k) > a)
> plot(dat(k,1),dat(k,2),'g.');
> end
>
> Why this is giving me an empty plot all the time?
Hi Anoop, The way you have written the code, how is that if statement supposed to evaluate? r2(k) is a vector, r1(k) is a vector (presumably if there is more than one index in dat(:,3) that is not equal to zero). So you are asking if a vector is less than b (a scalar), and a vector is greater than a (another scalar). Try the way I've written it below with some junk data input and see if it makes sense why that if statement cannot work.

dat = randn(1024,3); %creating some junk data
Y = randsample(1024,50); % just to make some zeros randsample() in stats toolbox
dat(Y,3) = 0;
r1 = dat(:,1);
r2 = dat(:,2);
 a = 0.86;
 b = 0.85;
k = find(dat(:,3) ~= 0);
sig_values = find(r2(k) < b & r1(k) > a);
plot(r1(sig_values),r2(sig_values),'g.');

Hope that helps,
wayne

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us