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:
Simple question about matrices

Subject: Simple question about matrices

From: Till Kroeger

Date: 28 Oct, 2010 17:02:04

Message: 1 of 12

Hello!

I'm rather new to matlab (but not to IT). Since matlab has excellent built-in operator to avoid for-loops when dealing with matrices I tried the following (simplified) :

I have a four dimensional matrix: (essentially an image where for each pixel a 2x2 matrix is stored)

eg: v = zeros(2, 2, 200, 200);

What I want to do in a simple command is to write the 2x2 matrix for given pixel x, y without using a for loop.

I played around with the colon operator, but did not get any useful results.

Can you help me?

Subject: Simple question about matrices

From: Sean

Date: 28 Oct, 2010 17:11:03

Message: 2 of 12

"Till Kroeger" <inspirativity@googlemail.com> wrote in message <iacaac$fgk$1@fred.mathworks.com>...
> Hello!
>
> I'm rather new to matlab (but not to IT). Since matlab has excellent built-in operator to avoid for-loops when dealing with matrices I tried the following (simplified) :
>
> I have a four dimensional matrix: (essentially an image where for each pixel a 2x2 matrix is stored)
>
> eg: v = zeros(2, 2, 200, 200);
>
> What I want to do in a simple command is to write the 2x2 matrix for given pixel x, y without using a for loop.
>
> I played around with the colon operator, but did not get any useful results.
>
> Can you help me?

You'll need to provide us with the criteria for assigning that 2x2 matrix. Does it come from a list, a spreadsheet, a function of 11 random integers?

Also, for ease of use I think I would have the matrix be 200x200x2x2 instead of the other way around. This is easy enough to PERMUTE though.

Subject: Simple question about matrices

From: someone

Date: 28 Oct, 2010 17:16:04

Message: 3 of 12

"Till Kroeger" <inspirativity@googlemail.com> wrote in message <iacaac$fgk$1@fred.mathworks.com>...
> Hello!
>
> I'm rather new to matlab (but not to IT). Since matlab has excellent built-in operator to avoid for-loops when dealing with matrices I tried the following (simplified) :
>
> I have a four dimensional matrix: (essentially an image where for each pixel a 2x2 matrix is stored)
>
> eg: v = zeros(2, 2, 200, 200);
>
> What I want to do in a simple command is to write the 2x2 matrix for given pixel x, y without using a for loop.
>
> I played around with the colon operator, but did not get any useful results.
>
> Can you help me?

% Perhaps:

doc permute
doc reshape

Subject: Simple question about matrices

From: Till Kroeger

Date: 28 Oct, 2010 17:46:15

Message: 4 of 12

some more information:

I four matrices (n x m) (lets call them g1,g2,g3,g4 which hold information (double) for one position in an image (n x m).

What I want to do now (without for-loops) for each position (x,y) in my original image is to combine the four corresponding scalar values from g1(x,y),g2(x,y),g3(x,y),g4(x,y) in a 2x2 matrix (first row g1 and g2, second row g3,g4).

This will result in n times m 2x2 matrices. These matrices should be kept in one data type (eg. (n, m, 2, 2)).

My current version looks like this:

C = zeros(size(img,1), size(img,2), 2, 2);

C(:,:,:,:) = [g1, g2; g3, g4];

However I always get a dimension mismatch error.

Subject: Simple question about matrices

From: Till Kroeger

Date: 28 Oct, 2010 18:04:46

Message: 5 of 12

Sorry for the typos. The first phrase should read:

I have four matrices (each of them n x m) (lets call them g1,g2,g3,g4) which hold information (double) for one position of an (n x m) image.

Subject: Simple question about matrices

From: Sean

Date: 28 Oct, 2010 18:20:06

Message: 6 of 12

Till Kroeger <inspirativity@googlemail.com> wrote in message <301616218.196152.1288289116525.JavaMail.root@gallium.mathforum.org>...
> Sorry for the typos. The first phrase should read:
>
> I have four matrices (each of them n x m) (lets call them g1,g2,g3,g4) which hold information (double) for one position of an (n x m) image.

%sample data
g1 = reshape(randperm(25),1,[]);
g2 = reshape(randperm(25),1,[]);
g3 = reshape(randperm(25),1,[]);
g4 = reshape(randperm(25),1,[]);

%assignment: to a m*2xn*2 image:
fullMat = zeros(5*2);
fullMat(1:4:end)=g1;
fullMat(2:4:end)=g2;
fullMat(3:4:end)=g3;
fullMat(4:4:end)=g4;

%Restructure to what you want:
fullMat = permute(reshape(fullMat,2,2,5,5),[2 1 3 4]);

%Assert it did what we want:
isequal([g1(1) g2(1);g3(1) g4(1)],fullMat(:,:,1,1))

Subject: Simple question about matrices

From: James Tursa

Date: 28 Oct, 2010 18:32:04

Message: 7 of 12

Till Kroeger <inspirativity@googlemail.com> wrote in message <1162995368.196098.1288288005729.JavaMail.root@gallium.mathforum.org>...
> some more information:
>
> I four matrices (n x m) (lets call them g1,g2,g3,g4 which hold information (double) for one position in an image (n x m).
>
> What I want to do now (without for-loops) for each position (x,y) in my original image is to combine the four corresponding scalar values from g1(x,y),g2(x,y),g3(x,y),g4(x,y) in a 2x2 matrix (first row g1 and g2, second row g3,g4).
>
> This will result in n times m 2x2 matrices. These matrices should be kept in one data type (eg. (n, m, 2, 2)).
>
> My current version looks like this:
>
> C = zeros(size(img,1), size(img,2), 2, 2);
>
> C(:,:,:,:) = [g1, g2; g3, g4];
>
> However I always get a dimension mismatch error.

Whether you should have the data stored as a (n,m,2,2) or as a (2,2,n,m) depends on how you use the data downstream in your code. Will you typically be working with the 2x2 slices or with the nxm slices? Generally I would advise that your slices be the first two dimensions.

As far as your specific question is concerned above you can use linear indexing:

 C(:) = [g1(:); g2(:); g3(:); g4(:)];

or you can save a bit of internal copying and skip the pre-allocation:

 C = reshape([g1(:); g2(:); g3(:); g4(:)],size(img,1), size(img,2), 2, 2);


James Tursa

Subject: Simple question about matrices

From: Till

Date: 29 Oct, 2010 08:42:36

Message: 8 of 12

Thanks!

C = reshape([g1(:); g2(:); g3(:); g4(:)],size(img,1), size(img,2), 2, 2);

works well!

I'll be using the 2x2 matrices to calculate the determinant for each pixel. If the determinant is above a certain constant threshold, the corresponding point will be stored (in a different array).

How can I access all matrices for all pixels in such way that I can write

if (det( of 2x2 matrix at point x,y from array C)>t)
    store corresponding x,y
end

I'm currently doing that with a for-loop. But every time I run my not-very-elegant algorithm, I'm dying a slow painful death.

Subject: Simple question about matrices

From: Jan Simon

Date: 29 Oct, 2010 09:13:04

Message: 9 of 12

Dear Till,

the descriptions of your problems are not easy to understand. If you have a working FOR-loop code, it would be very helpful, if you post it.

If I understood you correctly you have a [M x N x 2 x 2] array and want to calcultate the determinants:
  x = rand(100, 200, 2, 2);
  D = x(:, :, 1, 1) .* x(:, :, 2, 2) - x(:, :, 1, 2) .* x(:, :, 2, 1);
  HigherThanLimit = (D > 0.8);
Now HigherThanLimit is a logical matrix, which can be used for indexing another array.

But perhaps I didn't get your problem.

Kind regards, Jan

Subject: Simple question about matrices

From: Till

Date: 29 Oct, 2010 09:52:09

Message: 10 of 12

Sorry if my description was confusing.

The problem is not 'what' I want to do/calculate with the matrix, but rather 'how' I address those matrices without using for-loops.

I have an M x N x 2 x 2 matrix. And I want to save all indices M,N in a (separate) array if the corresponding 2x2 matrix has a certain property.

My (simplified) for loops is this:

for i=1:M
for j=1:N
  if (CalculateSomething(C(:,:,i,j) > threshold)
         Array(end+1,:) = [i,j];
end
end
end

I want to do this without a for-loop.

Subject: Simple question about matrices

From: Jan Simon

Date: 29 Oct, 2010 10:49:04

Message: 11 of 12

Dear Til,

> The problem is not 'what' I want to do/calculate with the matrix, but rather 'how' I address those matrices without using for-loops.

But I can show you just an example, if you define *what* should be calculated.

> I have an M x N x 2 x 2 matrix. And I want to save all indices M,N in a (separate) array if the corresponding 2x2 matrix has a certain property.
> for i =1:M
> for j =1:N
> %%% Wrong?! if (CalculateSomething(C(:,:,i,j) > threshold)
         if (CalculateSomething(C(i, j, :, :) > threshold)
> Array(end+1,:) = [i,j];
> end
> end
> end

Letting Array grow dynamically is very inefficient. Be sure that you *ever* preallocate the output.
I showed you in the ast post how to perform this wihout loops for the determinants of the trailing 2x2 matrices. The output is a logical index matrix, which is usually faster to use than a list of indices as your "Array".
Did you understand my example, does is solve your problem and if not, where are the differences to your needs?

Jan

Subject: Simple question about matrices

From: Till

Date: 29 Oct, 2010 11:46:25

Message: 12 of 12

I did not use preallocation because I don't know how many tupels of M and N will be found.

However, you're right. The dynamical allocation was the bottleneck. Your solution of splitting it up into a temporary M x N matrix with all determinants and subsequently a M x N logical matrix for thresholding seems to be the best solution.

Thanks again!

Tags for 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