Thread Subject: How to avoid for-loop in this problem?

Subject: How to avoid for-loop in this problem?

From: Kian

Date: 11 Aug, 2009 20:01:21

Message: 1 of 10

Hi,

I have a 3D matrix that contains 50 stimuli frames. Each frame contains 1024*1024 pixels. Therefore, my NoiseMatrix is a matrix of 50*1024*1024. I use this matrix to create smaller 600x800 frames. For this, I'd need a page number, an x-offset, and a y-offset.

Example:

3, 102, 253

Will give me a 600x800 pixel frame from the NoiseMatrix, like this:

frame = NoiseMatrix(3, 102:600, 253:800);

I have 100 frames and a matrix that contains 100 pages and 100 x-offsets and 100 y-offsets:

frameOffsets:

3 4 1 7 24 ... 82 page numbers
102 12 5 241 61 ... 31 x-offsets (100 long)
253 2 45 21 251 ... 21 y-offsets (100 long)


For 100 frames, I can easily to this with a for-loop:

for i = 1:100
 frames(i,1:600,1:800) = NoiseMatrix(frameOffsets(1,i), frameOffsets(2,i):frameOffsets(2,i)+600, frameOffsets(3,i):frameOffsets(3,i)+800);
end

This for loop will construct 100 frames by going through the NoiseMatrix and reconstructing frames using the data (page numbers, x-offsets, and y-offsets) in the frameOffsets matrix.

How can this be done without a for-loop?

Thanks in advance!

Subject: How to avoid for-loop in this problem?

From: someone

Date: 11 Aug, 2009 20:24:19

Message: 2 of 10

"Kian " <kian.torab@utah.edu> wrote in message <h5simh$g5i$1@fred.mathworks.com>...
> Hi,
>
> I have a 3D matrix that contains 50 stimuli frames. Each frame contains 1024*1024 pixels. Therefore, my NoiseMatrix is a matrix of 50*1024*1024. I use this matrix to create smaller 600x800 frames. For this, I'd need a page number, an x-offset, and a y-offset.
>
> Example:
>
> 3, 102, 253
>
> Will give me a 600x800 pixel frame from the NoiseMatrix, like this:
>
> frame = NoiseMatrix(3, 102:600, 253:800);
>
> I have 100 frames and a matrix that contains 100 pages and 100 x-offsets and 100 y-offsets:
>
> frameOffsets:
>
> 3 4 1 7 24 ... 82 page numbers
> 102 12 5 241 61 ... 31 x-offsets (100 long)
> 253 2 45 21 251 ... 21 y-offsets (100 long)
>
>
> For 100 frames, I can easily to this with a for-loop:
>
> for i = 1:100
> frames(i,1:600,1:800) = NoiseMatrix(frameOffsets(1,i), frameOffsets(2,i):frameOffsets(2,i)+600, frameOffsets(3,i):frameOffsets(3,i)+800);
> end
>
> This for loop will construct 100 frames by going through the NoiseMatrix and reconstructing frames using the data (page numbers, x-offsets, and y-offsets) in the frameOffsets matrix.
>
> How can this be done without a for-loop?
>
> Thanks in advance!

Something doesn't quite make sense here (to me).
You have a NoiseMatrix of 50 pages.
You have frameOffsets of 82 page numbers.
Yet you loop from 1 to 100.

Am I missing something?

Also, as long as you preallocate frames with something like:
frames = zeros(100,600,800)
before the for loop, the loop method
MAY be as efficient as a "vectorized" method.

Subject: How to avoid for-loop in this problem?

From: Kian

Date: 11 Aug, 2009 21:21:01

Message: 3 of 10

Sorry, I have 100 pages. 82 is the last number in the vector. It belongs to 31 and 21 for x-values and y-values. It should read like this:

3 4 1 7 24 ... 82 -- page numbers for the 100 pages
102 12 5 241 61 ... 31 -- x-offsets (100 long)
253 2 45 21 251 ... 21 -- y-offsets (100 long)

Kian

Subject: How to avoid for-loop in this problem?

From: Bruno Luong

Date: 11 Aug, 2009 22:12:00

Message: 4 of 10

"Kian " <kian.torab@utah.edu> wrote in message <h5simh$g5i$1@fred.mathworks.com>...
> Hi,
>
> I have a 3D matrix that contains 50 stimuli frames. Each frame contains 1024*1024 pixels. Therefore, my NoiseMatrix is a matrix of 50*1024*1024. I use this matrix to create smaller 600x800 frames. For this, I'd need a page number, an x-offset, and a y-offset.
>
> Example:
>
> 3, 102, 253
>
> Will give me a 600x800 pixel frame from the NoiseMatrix, like this:
>
> frame = NoiseMatrix(3, 102:600, 253:800);
>
> I have 100 frames and a matrix that contains 100 pages and 100 x-offsets and 100 y-offsets:
>
> frameOffsets:
>
> 3 4 1 7 24 ... 82 page numbers
> 102 12 5 241 61 ... 31 x-offsets (100 long)
> 253 2 45 21 251 ... 21 y-offsets (100 long)
>
>
> For 100 frames, I can easily to this with a for-loop:
>
> for i = 1:100
> frames(i,1:600,1:800) = NoiseMatrix(frameOffsets(1,i), frameOffsets(2,i):frameOffsets(2,i)+600, frameOffsets(3,i):frameOffsets(3,i)+800);
> end
>

It can be do without loop, but I don't think there is any speed gain. The code will be less readable. My suggestion is to stay with for-loop.

Bruno

Subject: How to avoid for-loop in this problem?

From: Kian

Date: 11 Aug, 2009 22:30:18

Message: 5 of 10

Really? I have 1000 trials and each trial has 100 frames to reconstruct, so I end up going through this loop 100,000 times. Would it still not help any?

I pre-allocated my variable and it was a lot faster.

Thanks to everyone for your help.

Kian

Subject: How to avoid for-loop in this problem?

From: Bruno Luong

Date: 11 Aug, 2009 23:24:19

Message: 6 of 10

"Kian " <kian.torab@utah.edu> wrote in message <h5srdq$loh$1@fred.mathworks.com>...
> Really? I have 1000 trials and each trial has 100 frames to reconstruct, so I end up going through this loop 100,000 times. Would it still not help any?
>

Test yourself:

% Vectorized engine
ny = 600; nx = 800;
[F Y X] = ndgrid(1,1:ny,1:nx);
idxlin = sub2ind(size(NoiseMatrix),F,Y,X);
tic
iOffsets = (frameOffsets(1,:)-1) + ...
                size(NoiseMatrix,1)*((frameOffsets(2,:)-1) + ...
                     size(NoiseMatrix,2)*(frameOffsets(3,:)-1));
frames = NoiseMatrix(bsxfun(@plus, idxlin, iOffsets(:)));
toc

% For loop engine
frames = zeros([size(frameOffsets,2) ny nx]);
tic
for i = 1:size(frameOffsets,2)
  frames(i,:,:) = NoiseMatrix(frameOffsets(1,i), ...
                                    frameOffsets(2,i)+(0:ny-1), ...
                                    frameOffsets(3,i)+(0:nx-1));
end
toc

% Bruno

Subject: How to avoid for-loop in this problem?

From: Kian

Date: 13 Aug, 2009 20:00:24

Message: 7 of 10

Hi Bruno,

Thanks for your help!

Somehow I get a negative index, so the last line fails for the vectorized method. Is there a way to attach a MATLAB file to this forum? That way I can send you the NoiseMatrix and also the offsets so you can see it for yourself. I tried, but I couldn't figure out your code.

This vectorized method is very useful. I hope I can figure it out.

Thanks,
Kian

Subject: How to avoid for-loop in this problem?

From: Kian

Date: 13 Aug, 2009 20:02:19

Message: 8 of 10

My email address is kian.torab@utah.edu. If you email me I'll send you the MAT file.

Thanks!

Subject: How to avoid for-loop in this problem?

From: Bruno Luong

Date: 13 Aug, 2009 20:30:21

Message: 9 of 10

"Kian " <kian.torab@utah.edu> wrote in message <h61rgb$27n$1@fred.mathworks.com>...
> My email address is kian.torab@utah.edu. If you email me I'll send you the MAT file.
>
> Thanks!

Kian, my email is not working today. But I'm pretty sure the negative indices happen because your offset convention different than mine. I assume offsets started from 1 (offset -> indices of the first corners of the subarray). If you have different convention (zero based), then add 1 to your offset before calling the code.

Bruno

Subject: How to avoid for-loop in this problem?

From: Kian

Date: 14 Aug, 2009 18:42:03

Message: 10 of 10

Hi Bruno,

I don't think that's the problem because the indices are all greater than 100.

Here's a link to the NoiseMatrix and frameOffsets data files in case you'd like to see this yourself.

Basically, the NoiseMatrix contains 50 of 1024x1024 pages, containing 1s and 0s. The frameOffsets refers to smaller rectangular regions within those pages. For example, the first column of the frameOffsets refers to a box that contains a box with the upper left corner of (215, 420) and lower right corner of (1015, 1020). This smaller rectangle comes from the 39th page (the first index of the NoiseMatrix array).

Or in short, it refers to:
NoiseMatrix(39, 215:1015, 420:1020);

Next page:
NoiseMatrix(23, 223:1023, 348:948);

MAT Data is located at: http://files.me.com/kianabc/9zryy5

See if you can figure it out if you have the time.

Thanks in advance,
Kian

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
for loop Kian 11 Aug, 2009 16:04:19
rssFeed for this Thread

Contact us at files@mathworks.com