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:
Skip lines in a for loop

Subject: Skip lines in a for loop

From: Maxx Chatsko

Date: 12 Jul, 2010 18:22:05

Message: 1 of 10

Hello all,
   Suppose I have a double with 15 rows. I want to take data from the first four lines in every set of five and skip the fifth row. How can I express this in a for loop? Thanks
                 Maxx

Subject: Skip lines in a for loop

From: ImageAnalyst

Date: 12 Jul, 2010 18:24:44

Message: 2 of 10

Maxx"
Do you want each iteration to take 4 rows and skip the 5th, OR do you
want that to happen over 5 iterations (so that you're only taking one
row per iteration)?

Subject: Skip lines in a for loop

From: Andy

Date: 12 Jul, 2010 18:30:22

Message: 3 of 10

"Maxx Chatsko" <chatskom@chemimage.com> wrote in message <i1fmgd$hs8$1@fred.mathworks.com>...
> Hello all,
> Suppose I have a double with 15 rows. I want to take data from the first four lines in every set of five and skip the fifth row. How can I express this in a for loop? Thanks
> Maxx

A for loop can run over any array, not just a consecutive array. That is, instead of:

for ix = 1:15
 ...
end

you can use:

for ix = [1:4 6:9 11:14] % only first four of each five indices
 ...
end

Subject: Skip lines in a for loop

From: Ashish Uthama

Date: 12 Jul, 2010 18:31:12

Message: 4 of 10

On Mon, 12 Jul 2010 14:22:05 -0400, Maxx Chatsko <chatskom@chemimage.com>
wrote:

> Hello all,
> Suppose I have a double with 15 rows. I want to take data from the
> first four lines in every set of five and skip the fifth row. How can I
> express this in a for loop? Thanks
> Maxx

Do you need a FOR loop? It would have helped to give more context.

If the data is not large, you could do this:

a=(1:15)' %example data with 15 rows
b=a; %make a copy
b(5:5:end,:)=[] %delete every 5th row

Subject: Skip lines in a for loop

From: Maxx Chatsko

Date: 12 Jul, 2010 18:35:05

Message: 5 of 10

ImageAnalyst <imageanalyst@mailinator.com>
> Maxx"
> Do you want each iteration to take 4 rows and skip the 5th, OR do you
> want that to happen over 5 iterations (so that you're only taking one
> row per iteration)?

     I would like to take 4 rows and skip the 5th for each iteration.

Subject: Skip lines in a for loop

From: Frédéric Bergeron

Date: 12 Jul, 2010 18:37:04

Message: 6 of 10

"Maxx Chatsko" <chatskom@chemimage.com> wrote in message <i1fmgd$hs8$1@fred.mathworks.com>...
> Hello all,
> Suppose I have a double with 15 rows. I want to take data from the first four lines in every set of five and skip the fifth row. How can I express this in a for loop? Thanks
> Maxx

Hi,
maybe a for loop isn't necessary. Try this:

a=your data;
index=1:size(a,1);
b=a(rem(idx,5)~=0,:);

I think this is one of the solution.
Fred

Subject: Skip lines in a for loop

From: Maxx Chatsko

Date: 12 Jul, 2010 18:53:20

Message: 7 of 10

"Ashish Uthama" <first.last@mathworks.com>
>
> Do you need a FOR loop? It would have helped to give more context.

     Yeah good point. Sorry. So I have an array that holds spectral data from several ROIs, where each row represents an ROI. The user specifies a number of spectra and the program goes through a number of images, takes ROIs (spectra), and plots them. The problem with our program was if the user requested 22 spectra (or a number that wasn't a multiple of 5) from 5 images, the program plotted only 20 spectra (it was rounded to a clean number, so 4 ROIs from each image).
    Now it takes one extra ROI from 2 images, which is the remainder (22-20). Instead of just taking the next 12 spectrum in the array I want to take 4 from each image, thus the skipping one row in every 5 rows. Skipping the row will make each image count more uniformly towards the overall average of all spectra displayed.
   The number of spectra requested by the user will vary, so the remainder could be 1,2,3 etc. The number of images being processed will also change. I have the variables set, so that shouldn't be a problem.

Subject: Skip lines in a for loop

From: Maxx Chatsko

Date: 12 Jul, 2010 19:11:05

Message: 8 of 10

     So I guess my real question isn't about for loops, but about passing in rows of data from an array of data into an array of zeros. If I have

       zero_array(min:max,:)=data_array(???)

 is there a way to skip every fourth row in five when passing the data into the new array?
               Maxx
I'll be more precise next time. Promise.

Subject: Skip lines in a for loop

From: Ashish Uthama

Date: 12 Jul, 2010 19:32:00

Message: 9 of 10

On Mon, 12 Jul 2010 14:53:20 -0400, Maxx Chatsko <chatskom@chemimage.com>
wrote:

> "Ashish Uthama" <first.last@mathworks.com>
>> Do you need a FOR loop? It would have helped to give more context.
>
> Yeah good point. Sorry. So I have an array that holds spectral data
> from several ROIs, where each row represents an ROI. The user specifies
> a number of spectra and the program goes through a number of images,
> takes ROIs (spectra), and plots them. The problem with our program was
> if the user requested 22 spectra (or a number that wasn't a multiple of
> 5) from 5 images, the program plotted only 20 spectra (it was rounded to
> a clean number, so 4 ROIs from each image). Now it takes one extra
> ROI from 2 images, which is the remainder (22-20). Instead of just
> taking the next 12 spectrum in the array I want to take 4 from each
> image, thus the skipping one row in every 5 rows. Skipping the row will
> make each image count more uniformly towards the overall average of all
> spectra displayed.
> The number of spectra requested by the user will vary, so the
> remainder could be 1,2,3 etc. The number of images being processed will
> also change. I have the variables set, so that shouldn't be a problem.

That helps.

See if this makes sense (I get the feeling I might have complicated this
beyond needed.. :()

nImages=7;
nRoisPerImage=11;

%random data: (5 is the number of columns in your ROI/spectra
data=repmat((1:nImages*nRoisPerImage)', [1 5]);

nRoisReq = 17;

%i.e floor(17/7) ROIs from each image with mod(17,7) ROIs more needed


%full rois (floor(17/7))
RoisPerImageReg = repmat(floor(nRoisReq/nImages),[nImages,1]);

%we need to pick some additional rois, lets do that by picking up 1 more
%ROI for the first mod(17,7) images.
additionalRois = mod(nRoisReq,nImages);
RoisPerImageReg(1:additionalRois)= RoisPerImageReg(1:additionalRois) +1;

%Now lets create the start and stop indices into the main data array to
%extract these rois
startingInds = 1:nRoisPerImage:size(data,1);
startingInds = startingInds';
endingInds = startingInds+RoisPerImageReg-1;

%preallocate output
extractedData = zeros(nRoisReq,5);

%marker marks the section of the output (extractedData) that we want to
%write to.

markerStart=1;
for imageInd = 1:nImages
     markerEnd = markerStart+RoisPerImageReg(imageInd)-1;
     extractedData(markerStart:markerEnd,:) = ...
         data(startingInds(imageInd) : endingInds(imageInd),:);
     markerStart = markerEnd+1;
end

Subject: Skip lines in a for loop

From: Maxx Chatsko

Date: 12 Jul, 2010 20:42:04

Message: 10 of 10

"Ashish Uthama" <first.last@mathworks.com>

      A little complicated, so I did not use it. BUT, you got me thinking in the right direction and you and I figured it out. Thanks Ashish!
              Maxx

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