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:
For loop

Subject: For loop

From: Barry

Date: 1 Sep, 2010 13:33:21

Message: 1 of 12

a1 , a2 , a3 are 1x4 matices of integers between 1-6

max value a1 = 5, a2 = 4 and a3 = 3 (this is so it wouldn't be bigger than 6

D is a 6x4 matrix of values
I want to treat each column seperately but do the same thing. (So a for loop)

I want to sum the first column from a1 to a1+1, a2 to a2+2, a3 to a3+3 until it is larger than value S the first one larger will output text a1, a2, a3 to the first row first column of aa matrix size [1x4].

Similarly along doing the same for the second column taking values from column 2 of a(subscript ie. a1,a2,a3) and outputting to column 2 of aa

aa=zeros(size(a1));

So far
 for i=1:4
        for ii=1:3
             if sum(D(a(ii)+(0:(ii)-1),i))>S
             aa=a(ii);
             end
        end
 end

Thanks in advance for any help and also thanks for taking the time to read this if you could not help.
Kind Regards,
Barry

Subject: For loop

From: dpb

Date: 1 Sep, 2010 13:49:14

Message: 2 of 12

Barry wrote:
> a1 , a2 , a3 are 1x4 matices of integers between 1-6
...
> I want to sum the first column from a1 to a1+1, a2 to a2+2, a3 to a3+3
> until it is larger than value S the first one larger will output text
> a1, a2, a3 to the first row first column of aa matrix size [1x4].
> Similarly along doing the same for the second column taking values from
> column 2 of a(subscript ie. a1,a2,a3) and outputting to column 2 of aa
...

Trying to parse that broke me early this AM...

How about an example w/ expected results to follow?

--

Subject: For loop

From: Barry

Date: 1 Sep, 2010 14:29:06

Message: 3 of 12

i=1:4
ii=1:3

a1=[2,3,2,4]
a2=[1,3,2,3]
a3=[3,1,2,2]

aa=[0,0,0,0]

S=600

D=

100 0 150 100
650 550 200 100
300 200 250 200
200 450 200 200
400 100 400 150
100 150 500 100

1st column
using a1 value in column 1 (ie. 2) sum of a1 = 650 which is greater than > 600
aa(1,i)=a1
aa=[a1,0,0,0]

2nd column
using a1 value in column 2(ie. 3) sum of a1 = 200 which is not greater than > 600
using a2 value in column 2(ie. 3) sum of a2 to a2+1 =200+450>600

aa(1,i)=a2
aa=[a1,a2,0,0]

3rd column
using a1 value in column 3 sum of a1 = 200 which is not greater than > 600
using a2 value in column 3 sum of a2 to a2+1 =200+200 which is not greater than>600
using a2 value in column 3 sum of a3 to a3+2 =200+250+200 which is greater than>600

a3

aa=[a1,a2,a3,0]

3rd column
using a1 value in column 4 sum of a1 = 200 which is not greater than > 600
using a2 value in column 4 sum of a2 to a2+1 =200+250 which is not greater than>600
using a2 value in column 4 sum of a3 to a3+2 =100+200+200 which is not greater than>600

a3

aa=[a1,a2,a3,a3]

Subject: For loop

From: dpb

Date: 1 Sep, 2010 17:20:41

Message: 4 of 12

Barry wrote:
> i=1:4
> ii=1:3
>
> a1=[2,3,2,4]
> a2=[1,3,2,3]
> a3=[3,1,2,2]
> aa=[0,0,0,0]
> S=600
> D=
> 100 0 150 100
> 650 550 200 100
> 300 200 250 200
> 200 450 200 200
> 400 100 400 150
> 100 150 500 100
>
> 1st column using a1 value in column 1 (ie. 2) sum of a1 = 650 which is
> greater than > 600
> aa(1,i)=a1
> aa=[a1,0,0,0]

aa(1) = D(a1(1));

> 2nd column
> using a1 value in column 2(ie. 3) sum of a1 = 200 which is not greater
> than > 600

D(a1(2)) --> D(3) ==> 300, not 200.

Before we go on, let's make sure what we're actually doing...

--

Subject: For loop

From: Barry

Date: 1 Sep, 2010 18:53:21

Message: 5 of 12

> D=
> 100 0 150 100
> 650 550 200 100
> 300 200 250 200
> 200 450 200 200
> 400 100 400 150
> 100 150 500 100

That is for the second column so it would have been D(i,a1(2)) ---> D(2,3) sum=200

Subject: For loop

From: dpb

Date: 1 Sep, 2010 21:02:11

Message: 6 of 12

Barry wrote:
...

> 3rd column
> using a1 value in column 3 sum of a1 = 200 which is not greater than > 600
> using a2 value in column 3 sum of a2 to a2+1 =200+200 which is not
> greater than>600
> using a2 value in column 3 sum of a3 to a3+2 =200+250+200 which is
> greater than>600
>
> a3
>
> aa=[a1,a2,a3,0]
>
> 3rd column
> using a1 value in column 4 sum of a1 = 200 which is not greater than > 600
> using a2 value in column 4 sum of a2 to a2+1 =200+250 which is not
> greater than>600
> using a2 value in column 4 sum of a3 to a3+2 =100+200+200 which is not
> greater than>600
>
> a3
>
> aa=[a1,a2,a3,a3]

OK, I got the earlier question resolved; now the above--is the second
"3rd column" here a typo and should be "4th" instead?

Your original posting said nothing about the question of if there aren't
sufficient values to exceed S; is the result really to be the previous
value that did exceed S or the actual sum achieved w/ the data as found
or something else?

I'm asking if the repeated a3 above in aa is indeed correct or whether
it is also a type intended to be a4.

Clarity in specifying the rules doesn't appear to be a strong suit... :(

--

Subject: For loop

From: dpb

Date: 1 Sep, 2010 21:03:47

Message: 7 of 12

dpb wrote:
...

> Your original posting said nothing about the question of if there aren't
> sufficient values to exceed S; is the result really to be the previous
> value that did exceed S or the actual sum achieved w/ the data as found
> or something else?
>
> I'm asking if the repeated a3 above in aa is indeed correct or whether
> it is also a type intended to be a4.
...

And, of course, what if D doesn't contain values to satisfy the >S
condition to start with in column 1?

--

Subject: For loop

From: Barry

Date: 1 Sep, 2010 23:34:05

Message: 8 of 12

dpb <none@non.net> wrote in message <i5mf79$924$1@news.eternal-september.org>...
> Barry wrote:
> ...
>
> > 3rd column
> > using a1 value in column 3 sum of a1 = 200 which is not greater than > 600
> > using a2 value in column 3 sum of a2 to a2+1 =200+200 which is not
> > greater than>600
> > using a2 value in column 3 sum of a3 to a3+2 =200+250+200 which is
> > greater than>600
> >
> > a3
> >
> > aa=[a1,a2,a3,0]
> >
> > 3rd column
> > using a1 value in column 4 sum of a1 = 200 which is not greater than > 600
> > using a2 value in column 4 sum of a2 to a2+1 =200+250 which is not
> > greater than>600
> > using a2 value in column 4 sum of a3 to a3+2 =100+200+200 which is not
> > greater than>600
> >
> > a3
> >
> > aa=[a1,a2,a3,a3]
>
> OK, I got the earlier question resolved; now the above--is the second
> "3rd column" here a typo and should be "4th" instead?
>
> Your original posting said nothing about the question of if there aren't
> sufficient values to exceed S; is the result really to be the previous
> value that did exceed S or the actual sum achieved w/ the data as found
> or something else?
>
> I'm asking if the repeated a3 above in aa is indeed correct or whether
> it is also a type intended to be a4.
>
> Clarity in specifying the rules doesn't appear to be a strong suit... :(
>
> --

Ya sorry that is supposed to be 4th it was a typo as i just copied down the text above. yes it is supposed to be a3 aswell.

the first value which is above S is the value i want as it moves down generally the more numbers you sum will give a larger number i know this is not always true. and if none exceed S just take last value.

Subject: For loop

From: dpb

Date: 1 Sep, 2010 23:50:35

Message: 9 of 12

Barry wrote:
...

> Ya sorry that is supposed to be 4th it was a typo as i just copied down
> the text above. yes it is supposed to be a3 aswell.

OK, this is confusing, too. Is the a3 above a typo, too; if not, the "as
well" confuses in context. As well as what? Please be precise, I can't
tell what you're thinking except in the words you write and my crystal
ball is in the shop (yet again)...

> the first value which is above S is the value i want as it moves down
> generally the more numbers you sum will give a larger number i know this
> is not always true. and if none exceed S just take last value.

What is the "last value", specifically? The last column that _was_ >S
or the maximum sum available in the column under consideration?

And, the other question as a follow-on is dependent upon the answer
above and the question about a3, specifically.

--

Subject: For loop

From: Barry

Date: 2 Sep, 2010 09:04:22

Message: 10 of 12

dpb <none@non.net> wrote in message <i5mp32$t47$1@news.eternal-september.org>...
> Barry wrote:
> ...
>
> > Ya sorry that is supposed to be 4th it was a typo as i just copied down
> > the text above. yes it is supposed to be a3 aswell.
>
> OK, this is confusing, too. Is the a3 above a typo, too; if not, the "as
> well" confuses in context. As well as what? Please be precise, I can't
> tell what you're thinking except in the words you write and my crystal
> ball is in the shop (yet again)...
>
> > the first value which is above S is the value i want as it moves down
> > generally the more numbers you sum will give a larger number i know this
> > is not always true. and if none exceed S just take last value.
>
> What is the "last value", specifically? The last column that _was_ >S
> or the maximum sum available in the column under consideration?
>
> And, the other question as a follow-on is dependent upon the answer
> above and the question about a3, specifically.
>
> --
Call it the max sum available in the column but it will more than likely be the last one as it sums 4 cells..

Subject: For loop

From: Barry

Date: 2 Sep, 2010 11:49:08

Message: 11 of 12

I solved it the long way it is fine. needed it today just thought their might be a short way of doin thanks anyway for your time dpb..

Subject: For loop

From: dpb

Date: 2 Sep, 2010 13:10:33

Message: 12 of 12

Barry wrote:
> I solved it the long way it is fine. needed it today just thought their
> might be a short way of doin thanks anyway for your time dpb..

There might be _if_ one knew the rules well enough to be able to
actually do something...

Lesson being, no one at cs-sm will know what you're thinking other than
by what you post. To get good help (particularly timely good help)
requires good input on the problem description end.

I suspect w/ the special conditions a loop is the answer altho cumsum
may help. I think I'd turn the a_n arrays into a single
multi-dimensional array to index into rather than using different
variable names or a structure if were going to keep the names...

--

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