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:
Copy 2-D image array to form 3-D array

Subject: Copy 2-D image array to form 3-D array

From: Ramaprasad Kulkarni

Date: 9 Nov, 2010 01:19:04

Message: 1 of 5

Hi all,

My problem is similar to the one here:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/295543

I am using following steps to read and copy images to a 3-D matrix, but I don't know why I am still getting error:

>> a = imread('some_path');
>> size(a)
ans =
2000 2000
>> b(:,:,1) = a;
??? Subscripted assignment dimension mismatch. <-- I am getting this dimension mismatch error which is not expected.

Could anyone point out what the issue is (either from my side or something else !!)

--
Thanks,
Ram.

Subject: Copy 2-D image array to form 3-D array

From: Sean

Date: 9 Nov, 2010 01:44:04

Message: 2 of 5

"Ramaprasad Kulkarni" <prasad3130@gmail.com> wrote in message <iba7i8$5oo$1@fred.mathworks.com>...
> Hi all,
>
> My problem is similar to the one here:
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/295543
>
> I am using following steps to read and copy images to a 3-D matrix, but I don't know why I am still getting error:
>
> >> a = imread('some_path');
> >> size(a)
> ans =
> 2000 2000
> >> b(:,:,1) = a;
> ??? Subscripted assignment dimension mismatch. <-- I am getting this dimension mismatch error which is not expected.
>
> Could anyone point out what the issue is (either from my side or something else !!)
>
> --
> Thanks,
> Ram.

preallocate b

i.e.
b= zeros(2000,2000,number_of_omages,class(a));

b(:,:,1) = a;

Subject: Copy 2-D image array to form 3-D array

From: Walter Roberson

Date: 9 Nov, 2010 03:51:19

Message: 3 of 5

On 08/11/10 7:19 PM, Ramaprasad Kulkarni wrote:

> I am using following steps to read and copy images to a 3-D matrix, but
> I don't know why I am still getting error:
>
>>> a = imread('some_path');
>>> size(a)
> ans = 2000 2000
>>> b(:,:,1) = a;
> ??? Subscripted assignment dimension mismatch. <-- I am getting this
> dimension mismatch error which is not expected.
>
> Could anyone point out what the issue is (either from my side or
> something else !!)

You said that you are reading and copying images, implying more than
one. The second time through your loop, if the size of the second image
is not the same as the size of the first, you will get that error.

When b is not already allocated, then

b(:,:,1) = a;

is the same as

b(:,:) = a;

which is the same as

b = a;

(but only in this case where b is not already allocated.)


When b is already allocated, though, then

b(:,:,1) = a;

is the same as

b(1:size(b,1),1:size(b,2),1) = a;

and clearly that would not work if size(a,1) is not the same as
size(b,1) or if size(a,2) is not the same as size(b,2) .

In particular,

b(:,:,1) = a;

does *not* mean to copy a into the upper-right corner (or the lower left
corner) of plane 1 of b when b is already allocated and not the same
size as a.


My guess at the moment would be that you happen to already have a
variable b lying around and that it does not happen to be 2000 x 2000 x
whatever .


If you are wanting to read in a series of images and the images are not
all the same size, then you should probably read them in to a cell array
rather than trying to store them in to a single array.

Subject: Copy 2-D image array to form 3-D array

From: Ramaprasad Kulkarni

Date: 9 Nov, 2010 04:02:03

Message: 4 of 5

Thanks Sean, that was quick.

But I still didn't understand how preallocating alone would resolve it. Earlier, I had the same code snippet but just that it was not copied to a 3-D array.

--
Thanks
Ram.

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <iba914$88c$1@fred.mathworks.com>...
> "Ramaprasad Kulkarni" <prasad3130@gmail.com> wrote in message <iba7i8$5oo$1@fred.mathworks.com>...
> > Hi all,
> >
> > My problem is similar to the one here:
> > http://www.mathworks.com/matlabcentral/newsreader/view_thread/295543
> >
> > I am using following steps to read and copy images to a 3-D matrix, but I don't know why I am still getting error:
> >
> > >> a = imread('some_path');
> > >> size(a)
> > ans =
> > 2000 2000
> > >> b(:,:,1) = a;
> > ??? Subscripted assignment dimension mismatch. <-- I am getting this dimension mismatch error which is not expected.
> >
> > Could anyone point out what the issue is (either from my side or something else !!)
> >
> > --
> > Thanks,
> > Ram.
>
> preallocate b
>
> i.e.
> b= zeros(2000,2000,number_of_omages,class(a));
>
> b(:,:,1) = a;

Subject: Copy 2-D image array to form 3-D array

From: Ramaprasad Kulkarni

Date: 9 Nov, 2010 04:16:03

Message: 5 of 5

Thanks Walter and Sean.

Walter, your explanation is crystal clear and now I realize my mistake. I saw your post after I posted the last one, so pl ignore it.

--
Ram.


Walter Roberson <roberson@hushmail.com> wrote in message <Xy3Co.11445$E64.6553@newsfe11.iad>...
> On 08/11/10 7:19 PM, Ramaprasad Kulkarni wrote:
>
> > I am using following steps to read and copy images to a 3-D matrix, but
> > I don't know why I am still getting error:
> >
> >>> a = imread('some_path');
> >>> size(a)
> > ans = 2000 2000
> >>> b(:,:,1) = a;
> > ??? Subscripted assignment dimension mismatch. <-- I am getting this
> > dimension mismatch error which is not expected.
> >
> > Could anyone point out what the issue is (either from my side or
> > something else !!)
>
> You said that you are reading and copying images, implying more than
> one. The second time through your loop, if the size of the second image
> is not the same as the size of the first, you will get that error.
>
> When b is not already allocated, then
>
> b(:,:,1) = a;
>
> is the same as
>
> b(:,:) = a;
>
> which is the same as
>
> b = a;
>
> (but only in this case where b is not already allocated.)
>
>
> When b is already allocated, though, then
>
> b(:,:,1) = a;
>
> is the same as
>
> b(1:size(b,1),1:size(b,2),1) = a;
>
> and clearly that would not work if size(a,1) is not the same as
> size(b,1) or if size(a,2) is not the same as size(b,2) .
>
> In particular,
>
> b(:,:,1) = a;
>
> does *not* mean to copy a into the upper-right corner (or the lower left
> corner) of plane 1 of b when b is already allocated and not the same
> size as a.
>
>
> My guess at the moment would be that you happen to already have a
> variable b lying around and that it does not happen to be 2000 x 2000 x
> whatever .
>
>
> If you are wanting to read in a series of images and the images are not
> all the same size, then you should probably read them in to a cell array
> rather than trying to store them in to a single array.

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