Thread Subject: Help. I'm stuck on repeating for-loop.

Subject: Help. I'm stuck on repeating for-loop.

From: mat lav

Date: 3 Jan, 2008 20:55:10

Message: 1 of 8

The concept is really simple but I can't write the for-loop
that will do the job. In the for-loop, the upper limit
changes incrementally from 1 to 1000 and the results are
dumped into a matrix. Then these matrices are put into a
single cell. See below.

for i = 1 : 1;
    A1(i,1) = B(i,1);
end

for i = 1 : 2; %increments of 1; i.e., 1,2,3,4...1000
    A2(i,1) = B(i,1);
end

for i = 1 : 1000; %last value
    A1000(i,1) = B(i,1);
end

wrap = {A1, A2, A3, A4, A5 ..., A1000}
Obviously, you're not going to type it a 1000 times over.
But can't find an easier way to do this. I've gotten this,
but obviously it doesn't work:
for i = 1 : 1000;
    A{i,1}(i,1) = B(i,1);
end

Any help?

Subject: Help. I'm stuck on repeating for-loop.

From: Ken Fleisher

Date: 3 Jan, 2008 21:05:29

Message: 2 of 8

for ii = 1:1000
   A(ii,1) = B(ii,1);
end

You were close. Hope that helps.

Subject: Help. I'm stuck on repeating for-loop.

From: mat lav

Date: 3 Jan, 2008 22:09:25

Message: 3 of 8

NO. No offense, but you didn't read problem carefully. The code:
> for ii = 1:1000
> A(ii,1) = B(ii,1);
> end
will return just one matrix with a thousand rows.
What is required is a thousand matrices with the first
matrix having just 1 value (1 row), and the second matrix
having 2 rows, and the third matrix having 3 rows and so
forth up to 1000. Then these matrix are put into a cell. I
just don't know how to do it without typing it out a
thousand for loops - although the real solution is probably
a joke to a seasoned Matlab user.



"Ken Fleisher" <k-fleisher.donotspamme@nga.gov> wrote in
message <fljimo$9p$1@fred.mathworks.com>...
> for ii = 1:1000
> A(ii,1) = B(ii,1);
> end
>
> You were close. Hope that helps.

Subject: Help. I'm stuck on repeating for-loop.

From: Randy Poe

Date: 3 Jan, 2008 22:14:46

Message: 4 of 8

On Jan 3, 3:55 pm, "mat lav"
<mattylaver.delete.this.nosp...@gmail.com> wrote:
> The concept is really simple but I can't write the for-loop
> that will do the job. In the for-loop, the upper limit
> changes incrementally from 1 to 1000 and the results are
> dumped into a matrix. Then these matrices are put into a
> single cell. See below.
>
> for i = 1 : 1;
> A1(i,1) = B(i,1);
> end
>
> for i = 1 : 2; %increments of 1; i.e., 1,2,3,4...1000
> A2(i,1) = B(i,1);
> end
>
> for i = 1 : 1000; %last value
> A1000(i,1) = B(i,1);
> end
>
> wrap = {A1, A2, A3, A4, A5 ..., A1000}

If these are going to end up in a cell array, why define
the individual variables A1, A2, ...? Why not put
The values directly into your cell array?

> Obviously, you're not going to type it a 1000 times over.
> But can't find an easier way to do this. I've gotten this,
> but obviously it doesn't work:
> for i = 1 : 1000;
> A{i,1}(i,1) = B(i,1);

Try A{i} = B(1:i, 1);

> end

           - Randy

Subject: Help. I'm stuck on repeating for-loop.

From: someone

Date: 3 Jan, 2008 22:18:21

Message: 5 of 8

Randy Poe <poespam-trap@yahoo.com> wrote in message
<2f17e9ce-08c6-4fec-beca-
6be40cc1a29c@c4g2000hsg.googlegroups.com>...
> On Jan 3, 3:55 pm, "mat lav"
> <mattylaver.delete.this.nosp...@gmail.com> wrote:
> > The concept is really simple but I can't write the for-
loop
> > that will do the job. In the for-loop, the upper limit
> > changes incrementally from 1 to 1000 and the results are
> > dumped into a matrix. Then these matrices are put into a
> > single cell. See below.
> >
> > for i = 1 : 1;
> > A1(i,1) = B(i,1);
> > end
> >
> > for i = 1 : 2; %increments of 1; i.e., 1,2,3,4...1000
> > A2(i,1) = B(i,1);
> > end
> >
> > for i = 1 : 1000; %last value
> > A1000(i,1) = B(i,1);
> > end
> >
> > wrap = {A1, A2, A3, A4, A5 ..., A1000}
>
> If these are going to end up in a cell array, why define
> the individual variables A1, A2, ...? Why not put
> The values directly into your cell array?
>
> > Obviously, you're not going to type it a 1000 times
over.
> > But can't find an easier way to do this. I've gotten
this,
> > but obviously it doesn't work:
> > for i = 1 : 1000;
> > A{i,1}(i,1) = B(i,1);
>
> Try A{i} = B(1:i, 1);
>
> > end
>
> - Randy


I think Randy has it right. But if you really want to do
it the hard way, see Question 3.6 of the MATLAB FAQ at:

http://www.mit.edu/~pwb/cssm/matlab-faq_toc.html


Subject: Help. I'm stuck on repeating for-loop.

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 3 Jan, 2008 22:18:22

Message: 6 of 8

In article <fljmel$4a5$1@fred.mathworks.com>,
mat lav <mattylaver.delete.this.nospamy@gmail.com> wrote:

>What is required is a thousand matrices with the first
>matrix having just 1 value (1 row), and the second matrix
>having 2 rows, and the third matrix having 3 rows and so
>forth up to 1000. Then these matrix are put into a cell. I
>just don't know how to do it without typing it out a
>thousand for loops - although the real solution is probably
>a joke to a seasoned Matlab user.

If the above is your requirement, then either type out the
thousand for loops or use a program or editor to generate them
for you.

I would suggest, though, that you modify your strategy so that
instead of using matrices with 1 row, 2 rows, etc., that you use
a single cell, the K'th entry of which is the matrix with K rows.
Something along the lines of,

for K=1:1000
  A{K} = B(1:K,1);
end
--
   "Any sufficiently advanced bug is indistinguishable from a feature."
   -- Rich Kulawiec

Subject: Help. I'm stuck on repeating for-loop.

From: Peter Boettcher

Date: 3 Jan, 2008 23:12:10

Message: 7 of 8

"mat lav" <mattylaver.delete.this.nospamy@gmail.com> writes:

> The concept is really simple but I can't write the for-loop
> that will do the job. In the for-loop, the upper limit
> changes incrementally from 1 to 1000 and the results are
> dumped into a matrix. Then these matrices are put into a
> single cell. See below.
>
> for i = 1 : 1;
> A1(i,1) = B(i,1);
> end
>
> for i = 1 : 2; %increments of 1; i.e., 1,2,3,4...1000
> A2(i,1) = B(i,1);
> end
>
> for i = 1 : 1000; %last value
> A1000(i,1) = B(i,1);
> end
>
> wrap = {A1, A2, A3, A4, A5 ..., A1000}
> Obviously, you're not going to type it a 1000 times over.
> But can't find an easier way to do this. I've gotten this,
> but obviously it doesn't work:
> for i = 1 : 1000;
> A{i,1}(i,1) = B(i,1);
> end

I'm glad you recognize the cell array as the correct storage! The
simple coding solution is to use a second for loop

for j=1:1000
  for i=1:j
    A{j}(i) = B(i);
  end
end

But MATLAB matrix indexing is powerful:

for j=1:1000
  A{j} = B(1:j);
end

-Peter

Subject: Help. I'm stuck on repeating for-loop.

From: mat lav

Date: 3 Jan, 2008 23:36:24

Message: 8 of 8

Peter Boettcher <boettcher@ll.mit.edu> wrote in message
<muyhchubj1x.fsf@G99-Boettcher.llan.ll.mit.edu>...
> "mat lav" <mattylaver.delete.this.nospamy@gmail.com> writes:
>
> > The concept is really simple but I can't write the for-loop
> > that will do the job. In the for-loop, the upper limit
> > changes incrementally from 1 to 1000 and the results are
> > dumped into a matrix. Then these matrices are put into a
> > single cell. See below.
> >
> > for i = 1 : 1;
> > A1(i,1) = B(i,1);
> > end
> >
> > for i = 1 : 2; %increments of 1; i.e., 1,2,3,4...1000
> > A2(i,1) = B(i,1);
> > end
> >
> > for i = 1 : 1000; %last value
> > A1000(i,1) = B(i,1);
> > end
> >
> > wrap = {A1, A2, A3, A4, A5 ..., A1000}
> > Obviously, you're not going to type it a 1000 times over.
> > But can't find an easier way to do this. I've gotten this,
> > but obviously it doesn't work:
> > for i = 1 : 1000;
> > A{i,1}(i,1) = B(i,1);
> > end
>
> I'm glad you recognize the cell array as the correct
storage! The
> simple coding solution is to use a second for loop

The redundant sample code from my original post was just to
illustrate what I wanted to do - to lay out the concept. I
thought I was clear on that. There is no way anyone would
follow that approach. That's why I messed around with:
for i = 1 : 1000;
    A{i,1}(i,1) = B(i,1);
end
but couldn't get it work. It gave me the structure I wanted
- but with only point values (obviously).


>
> for j=1:1000
> for i=1:j
> A{j}(i) = B(i);
> end
> end
>
> But MATLAB matrix indexing is powerful:
>
> for j=1:1000
> A{j} = B(1:j);
> end
>
> -Peter

Yes, this method works. I didn't know about the matrix
indexing - I would never have guessed the name had you not
mentioned it for until now, from previous post, the syntax
B(1:j,1) looked like a typo.

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
forloop mat lav 3 Jan, 2008 15:59:59
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com