COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Skip lines in a for loop

• Follow

```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
```
 0
Reply Maxx 7/12/2010 6:22:05 PM

```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)?
```
 0
Reply ImageAnalyst 7/12/2010 6:24:44 PM

```"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
```
 0
Reply Andy 7/12/2010 6:30:22 PM

```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
```
 0
Reply Ashish 7/12/2010 6:31:12 PM

```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.
```
 0
Reply Maxx 7/12/2010 6:35:05 PM

```"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:

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

I think this is one of the solution.
Fred
```
 0
Reply Frédéric 7/12/2010 6:37:04 PM

```"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.
```
 0
Reply Maxx 7/12/2010 6:53:20 PM

```     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.
```
 0
Reply Maxx 7/12/2010 7:11:05 PM

```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.

%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
```
 0
Reply Ashish 7/12/2010 7:32:00 PM

```"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
```
 0
Reply Maxx 7/12/2010 8:42:04 PM

9 Replies
607 Views

(page loaded in 0.107 seconds)

Similiar Articles:

7/24/2012 12:30:52 AM