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:
if expression

Subject: if expression

From: majid

Date: 10 Nov, 2010 10:38:03

Message: 1 of 14

I have two matrices and I'm trying to write something that makes these two matrices the same. The problem with the code is that even though the if expression is true the if doesn't proceed to break statement. (i+d1) becomes equal to s1 but the if doesn't work. Do you know what's wrong?
Here's the code:

clc
d1 = 0;
d2 = 0;
y1 =[1;1;1;2;2;2;2;3;3];
yy1=[1;1;1;1;1;2;2;2;3;3;3];
s1 = length(y1);
s2 = length(yy1);
b = min (s1,s2);
bf = false;

for i=1:b
    
   m = i;
   while abs (y1(m)-yy1(m))>= 1
         
         if y1(m)<yy1(m)
            y1(m) = [];
            h1 = y1'
            m = m-1;
            d1 = d1+1;
                           
         else
             yy1(m) = [];
             h2 = yy1'
             m = m-1;
            d2 = d2+1;
         end
         
         if (i+d1) >= s1 || (i+d2) >= s2 % where I have problem with
            bf = true;
            break
         end
         
         m = m+1;
         
   end
    
    if bf %Get out of the FOR loop
          
       break
    end
end

Subject: if expression

From: Grzegorz Knor

Date: 10 Nov, 2010 11:54:03

Message: 2 of 14

"majid " <majid.sh.vaghefi@gmail.com> wrote in message <ibdsmb$k8g$1@fred.mathworks.com>...
> I have two matrices and I'm trying to write something that makes these two matrices the same. The problem with the code is that even though the if expression is true the if doesn't proceed to break statement. (i+d1) becomes equal to s1 but the if doesn't work. Do you know what's wrong?
> Here's the code:
>
> clc
> d1 = 0;
> d2 = 0;
> y1 =[1;1;1;2;2;2;2;3;3];
> yy1=[1;1;1;1;1;2;2;2;3;3;3];
> s1 = length(y1);
> s2 = length(yy1);
> b = min (s1,s2);
> bf = false;
>
> for i=1:b
>
> m = i;
> while abs (y1(m)-yy1(m))>= 1
>
> if y1(m)<yy1(m)
> y1(m) = [];
> h1 = y1'
> m = m-1;
> d1 = d1+1;
>
> else
> yy1(m) = [];
> h2 = yy1'
> m = m-1;
> d2 = d2+1;
> end
>
> if (i+d1) >= s1 || (i+d2) >= s2 % where I have problem with
> bf = true;
> break
> end
>
> m = m+1;
>
> end
>
> if bf %Get out of the FOR loop
>
> break
> end
> end

(i+d1) never becomes equal to s1 in your code. Add line:
        fprintf('i+d1 %.0f i+d2 %.0f s1 %.0f s2 %.0f\n',i+d1,i+d2,s1,s2)
before IF where you have problem and run your program.

Maybe you should set s1 and s2 as follows:
s2 = length(y1);
s1 = length(yy1);

best regards
Grzegorz

Subject: if expression

From: ImageAnalyst

Date: 10 Nov, 2010 11:54:03

Message: 3 of 14

How do you know? Did you set a breakpoint inside the if?

Subject: if expression

From: majid

Date: 10 Nov, 2010 12:36:03

Message: 4 of 14

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <0a1441ce-d1e6-4ad8-ac0a-88b0ef77beb4@g2g2000vbe.googlegroups.com>...
> How do you know? Did you set a breakpoint inside the if?

No but when I get the error i+d1 is 10. accounting that i has increased by 1 once more in the for loop before getting the error, i+d1 's been 9 in the previous loop which is equal to s1.

Subject: if expression

From: Grzegorz Knor

Date: 10 Nov, 2010 12:51:04

Message: 5 of 14

"majid " <majid.sh.vaghefi@gmail.com> wrote in message <ibe3jj$dc6$1@fred.mathworks.com>...
> ImageAnalyst <imageanalyst@mailinator.com> wrote in message <0a1441ce-d1e6-4ad8-ac0a-88b0ef77beb4@g2g2000vbe.googlegroups.com>...
> > How do you know? Did you set a breakpoint inside the if?
>
> No but when I get the error i+d1 is 10. accounting that i has increased by 1 once more in the for loop before getting the error, i+d1 's been 9 in the previous loop which is equal to s1.

But you have an error earlier, in line:
   while abs (y1(m)-yy1(m))>= 1

because m=9, but lenght(y1)=8

Grzegorz

Subject: if expression

From: PLH

Date: 10 Nov, 2010 13:30:24

Message: 6 of 14

Perhaps I've come in far too late in this discussion, but what do you by "I have 2 matrices that I want to make the same"? You clearly don't mean to just type A=B, and besides which, I only see vectors in your code...

I know this is not an helpful post, but I'm genuinely interested to understand what you mean.

Subject: if expression

From: majid

Date: 10 Nov, 2010 13:42:04

Message: 7 of 14


> But you have an error earlier, in line:
> while abs (y1(m)-yy1(m))>= 1
>
> because m=9, but lenght(y1)=8
>
> Grzegorz

That's my point. How could i or m becomes 9? in the previous loop i+d1 should have become equal to s1 (i = 8 and d1 = 1) and break the loop.

Subject: if expression

From: majid

Date: 10 Nov, 2010 13:54:04

Message: 8 of 14

"PLH " <paulhalkyard@googlemail.com> wrote in message <ibe6pg$950$1@fred.mathworks.com>...
> Perhaps I've come in far too late in this discussion, but what do you by "I have 2 matrices that I want to make the same"? You clearly don't mean to just type A=B, and besides which, I only see vectors in your code...
>
> I know this is not an helpful post, but I'm genuinely interested to understand what you mean.

You're right. matrices here are just for test. I have 3 different set of x-y values which I want to plot them in the same figure. the size of these x-y's are different but nearly around 30,000. the x's which are temperature are decreasing in the same pattern in all the 3 sets except that there are some missing points in different parts of them. so I'm trying to match the x's to be able to plot them with another program.

Subject: if expression

From: Grzegorz Knor

Date: 10 Nov, 2010 13:55:04

Message: 9 of 14

"majid " <majid.sh.vaghefi@gmail.com> wrote in message <ibe7fc$mlc$1@fred.mathworks.com>...
>
> > But you have an error earlier, in line:
> > while abs (y1(m)-yy1(m))>= 1
> >
> > because m=9, but lenght(y1)=8
> >
> > Grzegorz
>
> That's my point. How could i or m becomes 9? in the previous loop i+d1 should have become equal to s1 (i = 8 and d1 = 1) and break the loop.

When i==8
(y1(m)-yy1(m)) == 0
hence, the program does not enter the WHILE loop.

Subject: if expression

From: majid

Date: 10 Nov, 2010 14:09:04

Message: 10 of 14

"Grzegorz Knor" <gknor@o2.pl> wrote in message <ibe87o$eaj$1@fred.mathworks.com>...
> "majid " <majid.sh.vaghefi@gmail.com> wrote in message <ibe7fc$mlc$1@fred.mathworks.com>...
> >
> > > But you have an error earlier, in line:
> > > while abs (y1(m)-yy1(m))>= 1
> > >
> > > because m=9, but lenght(y1)=8
> > >
> > > Grzegorz
> >
> > That's my point. How could i or m becomes 9? in the previous loop i+d1 should have become equal to s1 (i = 8 and d1 = 1) and break the loop.
>
> When i==8
> (y1(m)-yy1(m)) == 0
> hence, the program does not enter the WHILE loop.

ohhh. Sure. that's it. Thanks a lot.

Subject: if expression

From: dpb

Date: 10 Nov, 2010 14:14:24

Message: 11 of 14

majid wrote:
>
>> But you have an error earlier, in line:
>> while abs (y1(m)-yy1(m))>= 1
>>
>> because m=9, but lenght(y1)=8
>>
>> Grzegorz
>
> That's my point. How could i or m becomes 9? in the previous loop i+d1
> should have become equal to s1 (i = 8 and d1 = 1) and break the loop.

That's one of those that stepping thru w/ the debugger will uncover your
logic error. I've looked briefly but it's convoluted enough code that
don't have time to dig into before a meeting...

I'd think outlining what you want for a result as another poster
requested would be a good start -- looks to me like there should be more
direct solution w/o

--

Subject: if expression

From: PLH

Date: 10 Nov, 2010 14:32:03

Message: 12 of 14

So you have:

Set 1: Vectors X (1xN1) and Y(1XN1)
Set 2: Vectors X (1xN2) and Y(1XN2)
Set 3: Vectors X (1xN3) and Y(1XN3)

where N1, N2 and N3 are approximately 30,000? And you know that the X values of each set should be the same but there are some missing? Can you presume the longest X vector can be trusted?

I may have misunderstood, but do you want to pad the y vectors where the x vectors have a "missing" point? If so, what about something like:

X1 = [1 1 1 2 2 2 2 3 3 3];
X2 = X1;

% randomly delete a random number of elements to simulate the different vectors
for j=1:ceil(5*rand)
    X2(ceil(rand*length(X2))) = [];
end

s = length(X1)-length(X2);
for j=1:s
    X2 = [X2 0]; % pad the small vector at the end
end

for j=1:s
    i = find(not(X2 == X1)); % find the first difference between the vectors
    % put in the correct value and omit the padded zero at the end
     X2 = [X2(1:i(1)-1) X1(i(1)) X2(i(1):end-1)];

     % now pad the y value
    Y2(i(1)) = 0;
end

This loops over a small number, which is the difference in length between the shortest and longest vector - you can straightforwardly generalise the above to include a set of vectors, I think.

If I'm completely off the plot, then my apologies!

Subject: if expression

From: PLH

Date: 10 Nov, 2010 14:56:03

Message: 13 of 14

Erm, I, of course, meant to pad the y-value with something like:

Y1 = [Y1(1:i(1)-1) 0 Y1(i(1):end)]

I.e., insert some value to show there's no signal/reading/whatever.

Subject: if expression

From: majid

Date: 10 Nov, 2010 16:05:05

Message: 14 of 14

"PLH " <paulhalkyard@googlemail.com> wrote in message <ibebq3$gal$1@fred.mathworks.com>...
> Erm, I, of course, meant to pad the y-value with something like:
>
> Y1 = [Y1(1:i(1)-1) 0 Y1(i(1):end)]
>
> I.e., insert some value to show there's no signal/reading/whatever.
Thanks for your reply. I solved my problem by Grzegorz tip. The way you did it doesn't work for me. I have to reduce the size of the X's to the smallest one (because of the kind of y values of have). your way makes all the X's look like the biggest one. Thank you.

Tags for this Thread

No tags are associated with 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