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:
double for loop problem

Subject: double for loop problem

From: Ondrej

Date: 28 Apr, 2009 00:44:02

Message: 1 of 5

Hi Guys, sorry to bother you with a stupid question, but I'm struggling here,... I have two arrayes with nn- number of columns,... in the first array.1 are the "values" and in the second array.2 are the "error for the values in the first array",... i.e. array.1(:,1) and array.2(:,1) belong together,... and i'm trying to make a simple loop which would put the values in one array together with the associated erros in the following way,...

results = array.1(:,1) , array.2(:,1), array.1(:,2), array.2(:,2),..... array.1(:,nn), array.2(:,nn) % i.e. the 1st column from array.1, the 2nd column is the 1st column from array.2, the 3rd column is the 2nd from array.1, the 4th column is the 2nd from array.2, etc.,...

for that i wrote the following script but it doesn't work, or it works in the way i don't want,...
 
results= [];
nn=4
n=nn.*2;

for jj=1:1:nn
for kk=1:2:n
spfit_dsp (:,kk) = spfit1_dsp (:,jj);
spfit_dsp (:,kk+1) = spfit1_dsp_err (:,jj);
 
end
end

Subject: double for loop problem

From: Roger Stafford

Date: 28 Apr, 2009 01:01:02

Message: 2 of 5

"Ondrej " <ondrej.muransky@ansto.gov.au> wrote in message <gt5jgi$3nl$1@fred.mathworks.com>...
> Hi Guys, sorry to bother you with a stupid question, but I'm struggling here,... I have two arrayes with nn- number of columns,... in the first array.1 are the "values" and in the second array.2 are the "error for the values in the first array",... i.e. array.1(:,1) and array.2(:,1) belong together,... and i'm trying to make a simple loop which would put the values in one array together with the associated erros in the following way,...
>
> results = array.1(:,1) , array.2(:,1), array.1(:,2), array.2(:,2),..... array.1(:,nn), array.2(:,nn) % i.e. the 1st column from array.1, the 2nd column is the 1st column from array.2, the 3rd column is the 2nd from array.1, the 4th column is the 2nd from array.2, etc.,...
>
> for that i wrote the following script but it doesn't work, or it works in the way i don't want,...
>
> results= [];
> nn=4
> n=nn.*2;
>
> for jj=1:1:nn
> for kk=1:2:n
> spfit_dsp (:,kk) = spfit1_dsp (:,jj);
> spfit_dsp (:,kk+1) = spfit1_dsp_err (:,jj);
>
> end
> end

  At first glance I see nothing wrong with your for-loop solution (except that you haven't put its results into 'results'. However 'reshape' offers a shorter solution.

 results = reshape([spfit1_dsp;spfit1_dsp_err],size(spfit1_dsp,1),[]);

Roger Stafford

Subject: double for loop problem

From: Ondrej

Date: 28 Apr, 2009 01:19:01

Message: 3 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gt5kge$s3q$1@fred.mathworks.com>...
> "Ondrej " <ondrej.muransky@ansto.gov.au> wrote in message <gt5jgi$3nl$1@fred.mathworks.com>...
> > Hi Guys, sorry to bother you with a stupid question, but I'm struggling here,... I have two arrayes with nn- number of columns,... in the first array.1 are the "values" and in the second array.2 are the "error for the values in the first array",... i.e. array.1(:,1) and array.2(:,1) belong together,... and i'm trying to make a simple loop which would put the values in one array together with the associated erros in the following way,...
> >
> > results = array.1(:,1) , array.2(:,1), array.1(:,2), array.2(:,2),..... array.1(:,nn), array.2(:,nn) % i.e. the 1st column from array.1, the 2nd column is the 1st column from array.2, the 3rd column is the 2nd from array.1, the 4th column is the 2nd from array.2, etc.,...
> >
> > for that i wrote the following script but it doesn't work, or it works in the way i don't want,...
> >
> > results= [];
> > nn=4
> > n=nn.*2;
> >
> > for jj=1:1:nn
> > for kk=1:2:n
> > spfit_dsp (:,kk) = spfit1_dsp (:,jj);
> > spfit_dsp (:,kk+1) = spfit1_dsp_err (:,jj);
> >
> > end
> > end
>
> At first glance I see nothing wrong with your for-loop solution (except that you haven't put its results into 'results'. However 'reshape' offers a shorter solution.
>
> results = reshape([spfit1_dsp;spfit1_dsp_err],size(spfit1_dsp,1),[]);
>
> Roger Stafford


Hi Roger,

problem is that by runing above script the "results" array contains only the nn-th columns from both arrays,... it looks that that "double for loop" doesn't run correctly (i.e. it is not wrriten correctly),... the "results" arrays currently looks like this: (if nn=4) results = array.1(:,nn-th), array.2(:,nn-th), array.1(:,nn-th), array.2(:,nn-th, etc.,... only the last columns from both arrays are there,...

Subject: double for loop problem

From: Roger Stafford

Date: 28 Apr, 2009 01:30:03

Message: 4 of 5

"Ondrej " <ondrej.muransky@ansto.gov.au> wrote in message <gt5li5$4kh$1@fred.mathworks.com>...
> problem is that by runing above script the "results" array contains only .....

  Yes, my "first glance" was not careful enough. (I'll blame it on old age.) This is really a problem for a single for-loop:

for jj=1:nn
 spfit_dsp(:,2*jj-1) = spfit1_dsp(:,jj);
 spfit_dsp(:,2*jj) = spfit1_dsp_err(:,jj);
end

  I still think you would like the 'reshape' solution once you get used to it.

Roger Stafford

Subject: double for loop problem

From: Ondrej

Date: 28 Apr, 2009 01:36:02

Message: 5 of 5

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gt5m6r$gq7$1@fred.mathworks.com>...
> "Ondrej " <ondrej.muransky@ansto.gov.au> wrote in message <gt5li5$4kh$1@fred.mathworks.com>...
> > problem is that by runing above script the "results" array contains only .....
>
> Yes, my "first glance" was not careful enough. (I'll blame it on old age.) This is really a problem for a single for-loop:
>
> for jj=1:nn
> spfit_dsp(:,2*jj-1) = spfit1_dsp(:,jj);
> spfit_dsp(:,2*jj) = spfit1_dsp_err(:,jj);
> end
>
> I still think you would like the 'reshape' solution once you get used to it.
>
> Roger Stafford

thank you very much Roger,... it works great,...
regards, ondrej

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