Deyan
Deyan

Reputation: 23

Store matrices in an array - MATLAB

I am trying to store 2x2 matrices in an array, which is to be connected to a particular size of an image for futher image processing. I.e.:

pixx = 300; % x number of pixels;
pixy = 200; % y number of pixels;
sz=zeros(pixx,pixy,4,4);
s=reshape(sz,[pixx,pixy,[2 2],4]);

This is how I try to fill the array with the 2x2 matrices:

s(:,:,[],1)=[1 1; 1 1]; % **
s(:,:,[],2)=[2 2; 2 2];
s(:,:,[],3)=[3 3; 3 3];
s(:,:,[],4)=[4 4; 4 4];

The double ** line produces the error: "Subscripted assignment dimension mismatch."

Would it be possible to give me a hint how to solve this issue?

Upvotes: 0

Views: 120

Answers (1)

Cris Luengo
Cris Luengo

Reputation: 60504

s(:,:,[],1) selects zero array elements.

If you were to use s(:,:,:,:,1) then you’d select the full matrix for each pixel, which of course is many more array elements than you have on the right side.

I think this is what you are trying to do:

s = zeros(1,1,2,2,4);
s(1,1,:,:,1) = [1 1; 1 1];
s(1,1,:,:,2) = [2 2; 2 2];
s(1,1,:,:,3) = [3 3; 3 3];
s(1,1,:,:,4) = [4 4; 4 4];
s = repmat(s,pixx,pixy,1,1,1);

Upvotes: 1

Related Questions