Thread Subject: codes for loops required...

Subject: codes for loops required...

From: Fuad Ahsan

Date: 17 Nov, 2008 15:23:02

Message: 1 of 13

let ....
A=[2 ;5 ;8 ;10 ;15]
B=[3 ;4 ;5 ;13 ;20]
now compare each element of matrix A with each element of matrix B
if the difference between the element of A and B is not >= 2 then discard it from A. or if the difference is >= 2 take the element of matrix A into another matrix C.

 the procces will be like this...........
abs(2-3)=1 ,not>=2 so dont take 2 from A
then
abs(5-3)=2, (>=2)
but abs(5-4)=1 ,not >=2 so dont take 5 also
then
abs(8-3)=5 .ok
abs(8-4)=4 .ok
abs (8-5)=3 .ok
abs(8-13)=5 ok
abs(8-20)=12 ok ... so now take 8 from matrix A.

finally for the given matrix A and B
C = [8 ;10; 15]

plz help me with the loop code.....

Subject: codes for loops required...

From: Adam

Date: 17 Nov, 2008 20:07:01

Message: 2 of 13

"Fuad Ahsan" <itsfuad01@hotmail.com> wrote in message <gfs28m$ng4$1@fred.mathworks.com>...
> let ....
> A=[2 ;5 ;8 ;10 ;15]
> B=[3 ;4 ;5 ;13 ;20]
> now compare each element of matrix A with each element of matrix B
> if the difference between the element of A and B is not >= 2 then discard it from A. or if the difference is >= 2 take the element of matrix A into another matrix C.
>
> the procces will be like this...........
> abs(2-3)=1 ,not>=2 so dont take 2 from A
> then
> abs(5-3)=2, (>=2)
> but abs(5-4)=1 ,not >=2 so dont take 5 also
> then
> abs(8-3)=5 .ok
> abs(8-4)=4 .ok
> abs (8-5)=3 .ok
> abs(8-13)=5 ok
> abs(8-20)=12 ok ... so now take 8 from matrix A.
>
> finally for the given matrix A and B
> C = [8 ;10; 15]
>
> plz help me with the loop code.....

This is pretty obvious homework because you would never use a loop. Could at least post what you tried if you're stuck.

C = A(abs(A-B)>2);

~Adam

Subject: codes for loops required...

From: someone

Date: 17 Nov, 2008 20:34:02

Message: 3 of 13

"Fuad Ahsan" <itsfuad01@hotmail.com> wrote in message <gfs28m$ng4$1@fred.mathworks.com>...
> let ....
> A=[2 ;5 ;8 ;10 ;15]
> B=[3 ;4 ;5 ;13 ;20]
> now compare each element of matrix A with each element of matrix B
> if the difference between the element of A and B is not >= 2 then discard it from A. or if the difference is >= 2 take the element of matrix A into another matrix C.
>
> the procces will be like this...........
> abs(2-3)=1 ,not>=2 so dont take 2 from A
> then
> abs(5-3)=2, (>=2)
> but abs(5-4)=1 ,not >=2 so dont take 5 also
> then
> abs(8-3)=5 .ok
> abs(8-4)=4 .ok
> abs (8-5)=3 .ok
> abs(8-13)=5 ok
> abs(8-20)=12 ok ... so now take 8 from matrix A.
>
> finally for the given matrix A and B
> C = [8 ;10; 15]
>
> plz help me with the loop code.....

% This should get you started...

for nB = 1:length(B)
   for nA = 1:length(A)
      if abs(A(nA) - B(nB)) >= 2
         % your turn, put your code here
      end
   end
end

Subject: codes for loops required...

From: Fuad Ahsan

Date: 18 Nov, 2008 09:52:02

Message: 4 of 13

> This is pretty obvious homework because you would never use a loop. Could at least post what you tried if you're stuck.
>
> C = A(abs(A-B)>2);
>
> ~Adam


///////////////////////////////////////////////

c1 = sort (randint (70,1,[200,800]))
c2 = sort (randint (70,1,[200,800]))

A1 = abs (c1-c2)
d_1 = find (A1>=2)
d1 = c1(d_1)
///////////////////////////////////////////////
 I did this code, but the problem is that it just compare the elements in same position of matrix c1 and c2.
but what i need is that compare each element of c1 with each element of c2. then if the difference is >=2 for all elements of c2 then take that element of c1.

now can u tell me how can i do this .
by the way its not a homework, i am trying to develop a code for channel assignment problem which i will use in Genetic algorithm (MOEA),

Subject: codes for loops required...

From: Adam

Date: 18 Nov, 2008 14:06:02

Message: 5 of 13

"Fuad Ahsan" <itsfuad01@hotmail.com> wrote in message <gfu382$kdg$1@fred.mathworks.com>...
> > This is pretty obvious homework because you would never use a loop. Could at least post what you tried if you're stuck.
> >
> > C = A(abs(A-B)>2);
> >
> > ~Adam
>
>
> ///////////////////////////////////////////////
>
> c1 = sort (randint (70,1,[200,800]))
> c2 = sort (randint (70,1,[200,800]))
>
> A1 = abs (c1-c2)
> d_1 = find (A1>=2)
> d1 = c1(d_1)
> ///////////////////////////////////////////////
> I did this code, but the problem is that it just compare the elements in same position of matrix c1 and c2.
> but what i need is that compare each element of c1 with each element of c2. then if the difference is >=2 for all elements of c2 then take that element of c1.
>
> now can u tell me how can i do this .
> by the way its not a homework, i am trying to develop a code for channel assignment problem which i will use in Genetic algorithm (MOEA),

Much better. :-)

c1 = floor(601*rand(70,1))+200;
c2 = floor(601*rand(70,1))+200;

mid = bsxfun(@minus, c1, c2');
d1 = all(abs(mid)>=2);

----

note: you could combine the last two lines, I just separated for clarity
d1 = all(abs(bsxfun(@minus, c1, c2'))>=2);

~Adam

Subject: codes for loops required...

From: Fuad Ahsan

Date: 18 Nov, 2008 18:02:01

Message: 6 of 13

> Much better. :-)
>
> c1 = floor(601*rand(70,1))+200;
> c2 = floor(601*rand(70,1))+200;
>
> mid = bsxfun(@minus, c1, c2');
> d1 = all(abs(mid)>=2);
>
> ----
>
> note: you could combine the last two lines, I just separated for clarity
> d1 = all(abs(bsxfun(@minus, c1, c2'))>=2);
>
> ~Adam


what the syntex "bsxfun" doing here?
i could not run it :-(

Subject: codes for loops required...

From: matt dash

Date: 18 Nov, 2008 18:19:02

Message: 7 of 13

"Fuad Ahsan" <itsfuad01@hotmail.com> wrote in message <gfuvup$5n7$1@fred.mathworks.com>...
> > Much better. :-)
> >
> > c1 = floor(601*rand(70,1))+200;
> > c2 = floor(601*rand(70,1))+200;
> >
> > mid = bsxfun(@minus, c1, c2');
> > d1 = all(abs(mid)>=2);
> >
> > ----
> >
> > note: you could combine the last two lines, I just separated for clarity
> > d1 = all(abs(bsxfun(@minus, c1, c2'))>=2);
> >
> > ~Adam
>
>
> what the syntex "bsxfun" doing here?
> i could not run it :-(

If you can't run it then your version of MATLAB is too old. You can download a replacement for bsxfun on the file exchange.

Subject: codes for loops required...

From: John D'Errico

Date: 18 Nov, 2008 18:25:03

Message: 8 of 13

"Fuad Ahsan" <itsfuad01@hotmail.com> wrote in message <gfuvup$5n7$1@fred.mathworks.com>...

> > note: you could combine the last two lines, I just separated for clarity
> > d1 = all(abs(bsxfun(@minus, c1, c2'))>=2);
> >
> > ~Adam
>
>
> what the syntex "bsxfun" doing here?
> i could not run it :-(

Because you have an old matlab release.

John

Subject: codes for loops required...

From: Adam

Date: 18 Nov, 2008 18:27:01

Message: 9 of 13

"matt dash" <n.a@mail.com> wrote in message <gfv0um$i2k$1@fred.mathworks.com>...
> "Fuad Ahsan" <itsfuad01@hotmail.com> wrote in message <gfuvup$5n7$1@fred.mathworks.com>...
> > > Much better. :-)
> > >
> > > c1 = floor(601*rand(70,1))+200;
> > > c2 = floor(601*rand(70,1))+200;
> > >
> > > mid = bsxfun(@minus, c1, c2');
> > > d1 = all(abs(mid)>=2);
> > >
> > > ----
> > >
> > > note: you could combine the last two lines, I just separated for clarity
> > > d1 = all(abs(bsxfun(@minus, c1, c2'))>=2);
> > >
> > > ~Adam
> >
> >
> > what the syntex "bsxfun" doing here?
> > i could not run it :-(
>
> If you can't run it then your version of MATLAB is too old. You can download a replacement for bsxfun on the file exchange.
>
 
or, as an alternative (aka the "old" way):
[C1 C2] = meshgrid(c1, c2);
d1 = all(abs(C1-C2) >= 2);

~Adam

Subject: codes for loops required...

From: Fuad Ahsan

Date: 18 Nov, 2008 21:27:02

Message: 10 of 13

> >
> > If you can't run it then your version of MATLAB is too old. You can download a replacement for bsxfun on the file exchange.
> >
>
> or, as an alternative (aka the "old" way):
> [C1 C2] = meshgrid(c1, c2);
> d1 = all(abs(C1-C2) >= 2);
>
> ~Adam


thanks

here d1 is a 1x70 matrix and if satisfy the condition (>= 2) fill the position by 1 otherwise 0.
but now how to find out the the element oc c1 whice satisfied the condition.
note: c1 is 70x1 matrix.
i mean i want to take only elements that are >=2 from elements of c2.
can you plz help me on this...........

Subject: codes for loops required...

From: Adam

Date: 18 Nov, 2008 22:19:03

Message: 11 of 13

"Fuad Ahsan" <itsfuad01@hotmail.com> wrote in message <gfvbv5$aul$1@fred.mathworks.com>...
> > >
> > > If you can't run it then your version of MATLAB is too old. You can download a replacement for bsxfun on the file exchange.
> > >
> >
> > or, as an alternative (aka the "old" way):
> > [C1 C2] = meshgrid(c1, c2);
> > d1 = all(abs(C1-C2) >= 2);
> >
> > ~Adam
>
>
> thanks
>
> here d1 is a 1x70 matrix and if satisfy the condition (>= 2) fill the position by 1 otherwise 0.
> but now how to find out the the element oc c1 whice satisfied the condition.
> note: c1 is 70x1 matrix.
> i mean i want to take only elements that are >=2 from elements of c2.
> can you plz help me on this...........

>> c1 = floor(601*rand(70,1))+200;
>> c2 = floor(601*rand(70,1))+200;
>> [C1 C2] = meshgrid(c1, c2);
>> d_1 = all(abs(C1-C2) >= 2);
>> d1 = c1(find(d_1))'

~Adam

Subject: codes for loops required...

From: Fuad Ahsan

Date: 19 Nov, 2008 17:38:02

Message: 12 of 13

"Adam" <not.real@email.com> wrote in message <gfvf0n$le6$1@fred.mathworks.com>...
>
> >> c1 = floor(601*rand(70,1))+200;
> >> c2 = floor(601*rand(70,1))+200;
> >> [C1 C2] = meshgrid(c1, c2);
> >> d_1 = all(abs(C1-C2) >= 2);
> >> d1 = c1(find(d_1))'
>
> ~Adam

thanks Adam
this function works perfectly , but i cann't use it several times in one cade , after some stage the memory cann't hold the values :-(
and show some errors.
btw i have the bsxfun function (downloaded from file ex.) but it also shows errors in associated file bsxarg.m

Subject: codes for loops required...

From: Adam

Date: 19 Nov, 2008 19:51:01

Message: 13 of 13

"Fuad Ahsan" <itsfuad01@hotmail.com> wrote in message <gg1itq$4pg$1@fred.mathworks.com>...
> "Adam" <not.real@email.com> wrote in message <gfvf0n$le6$1@fred.mathworks.com>...
> >
> > >> c1 = floor(601*rand(70,1))+200;
> > >> c2 = floor(601*rand(70,1))+200;
> > >> [C1 C2] = meshgrid(c1, c2);
> > >> d_1 = all(abs(C1-C2) >= 2);
> > >> d1 = c1(find(d_1))'
> >
> > ~Adam
>
> thanks Adam
> this function works perfectly , but i cann't use it several times in one cade , after some stage the memory cann't hold the values :-(
> and show some errors.
> btw i have the bsxfun function (downloaded from file ex.) but it also shows errors in associated file bsxarg.m

Did you look at what the individual steps are doing?

You're going to need to either re-use, or clear variables.

~Adam

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
not actually ho... Adam 18 Nov, 2008 09:18:25
homework Duane Hanselman 17 Nov, 2008 10:50:08
rssFeed for this Thread

Contact us at files@mathworks.com