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:
Storing multiple 2D arrays in a 3D array

Subject: Storing multiple 2D arrays in a 3D array

From: Basim Kakakhel

Date: 4 Nov, 2010 02:58:04

Message: 1 of 8

I have 60, 2D dicom images and i want to store them to a single 3 D array



Is the following code correct


if i am varying the Counter here from 1to 60.

Data(gel_Counter,:,:)=dicomread('Path');

regards

BASIM

Subject: Storing multiple 2D arrays in a 3D array

From: James Tursa

Date: 4 Nov, 2010 04:40:05

Message: 2 of 8

"Basim Kakakhel" <bkakakhail@hotmail.com> wrote in message <iat7fs$9o$1@fred.mathworks.com>...
> I have 60, 2D dicom images and i want to store them to a single 3 D array
>
>
>
> Is the following code correct
>
>
> if i am varying the Counter here from 1to 60.
>
> Data(gel_Counter,:,:)=dicomread('Path');
>
> regards
>
> BASIM

Although your way will work, generally it is better for memory storage and access to maintain the 2D blocks in a contiguous portion of memory. That means making them the first two dimensions. So I would advise this instead:

Data(:,:,gel_Counter)=dicomread('Path');

Also, be sure to preallocate Data first.

James Tursa

Subject: Storing multiple 2D arrays in a 3D array

From: Basim Kakakhel

Date: 4 Nov, 2010 05:27:04

Message: 3 of 8

Thanks for the help James.

One last question to add with this,

Whenever i have a dicom image i subtract it from a 2D water image.
Is the following syntax logically correct. i have used squeeze to convert the image into 2D so that it can be subtracted from water image which is a 2D array.


image_2d=squeeze(Data(gel_Counter,:,:))-water_Average;

Subject: Storing multiple 2D arrays in a 3D array

From: James Tursa

Date: 4 Nov, 2010 06:06:03

Message: 4 of 8

"Basim Kakakhel" <bkakakhail@hotmail.com> wrote in message <iatg78$m6h$1@fred.mathworks.com>...
> Thanks for the help James.
>
> One last question to add with this,
>
> Whenever i have a dicom image i subtract it from a 2D water image.
> Is the following syntax logically correct. i have used squeeze to convert the image into 2D so that it can be subtracted from water image which is a 2D array.
>
>
> image_2d=squeeze(Data(gel_Counter,:,:))-water_Average;

Yes. But again I will advise that you change your data arrangement to make the 2D arrays the first two dimensions and contiguous. You can already see that your current arrangement is causing you extra work to get this simple subtraction done. Each time you do it you have to traverse the entire Data array to get the pieces of the desired 2D array put together into a contiguous block in order to do the subtraction. It doesn't have to be that way. Suppose instead that you had the data arranged as I have suggested, with the 2D arrays the first two dimensions. Then the above could be accomplished without the squeeze as follows:

image_2d=Data(:,:,gel_Counter) - water_Average;

That looks cleaner and is easier to read. It is also more efficient for MATLAB to handle. In fact, thinking in a more MATLAB vectorized fashion, you could even do the whole subtraction for all the 2D arrays at once:

images = bsxfun(@minus, Data, water_Average);

The images variable will be a 3D array containing the 2D slices with water_Average subtracted out. Then you can pick off the resulting 2D arrays and work with them.

James Tursa

Subject: Storing multiple 2D arrays in a 3D array

From: Basim Kakakhel

Date: 4 Nov, 2010 06:25:03

Message: 5 of 8

Thanks James for your very useful comments.

BASIM



"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <iatigb$hu7$1@fred.mathworks.com>...
> "Basim Kakakhel" <bkakakhail@hotmail.com> wrote in message <iatg78$m6h$1@fred.mathworks.com>...
> > Thanks for the help James.
> >
> > One last question to add with this,
> >
> > Whenever i have a dicom image i subtract it from a 2D water image.
> > Is the following syntax logically correct. i have used squeeze to convert the image into 2D so that it can be subtracted from water image which is a 2D array.
> >
> >
> > image_2d=squeeze(Data(gel_Counter,:,:))-water_Average;
>
> Yes. But again I will advise that you change your data arrangement to make the 2D arrays the first two dimensions and contiguous. You can already see that your current arrangement is causing you extra work to get this simple subtraction done. Each time you do it you have to traverse the entire Data array to get the pieces of the desired 2D array put together into a contiguous block in order to do the subtraction. It doesn't have to be that way. Suppose instead that you had the data arranged as I have suggested, with the 2D arrays the first two dimensions. Then the above could be accomplished without the squeeze as follows:
>
> image_2d=Data(:,:,gel_Counter) - water_Average;
>
> That looks cleaner and is easier to read. It is also more efficient for MATLAB to handle. In fact, thinking in a more MATLAB vectorized fashion, you could even do the whole subtraction for all the 2D arrays at once:
>
> images = bsxfun(@minus, Data, water_Average);
>
> The images variable will be a 3D array containing the 2D slices with water_Average subtracted out. Then you can pick off the resulting 2D arrays and work with them.
>
> James Tursa

Subject: Storing multiple 2D arrays in a 3D array

From: JC

Date: 4 Nov, 2010 12:31:01

Message: 6 of 8

On Nov 4, 2:25 am, "Basim Kakakhel" <bkakakh...@hotmail.com> wrote:
> Thanks James for your very useful comments.
>
> BASIM
>
>
>
> "James Tursa" <aclassyguy_with_a_k_not_...@hotmail.com> wrote in message <iatigb$hu...@fred.mathworks.com>...
> > "Basim Kakakhel" <bkakakh...@hotmail.com> wrote in message <iatg78$m6...@fred.mathworks.com>...
> > > Thanks for the help James.
>
> > > One last question to add with this,
>
> > > Whenever i have a dicom image i subtract it from a 2D water image.
> > > Is the following syntax logically correct. i have used squeeze to convert the image into 2D so that it can be subtracted from water image which is a 2D array.
>
> > > image_2d=squeeze(Data(gel_Counter,:,:))-water_Average;
>
> > Yes. But again I will advise that you change your data arrangement to make the 2D arrays the first two dimensions and contiguous. You can already see that your current arrangement is causing you extra work to get this simple subtraction done. Each time you do it you have to traverse the entire Data array to get the pieces of the desired 2D array put together into a contiguous block in order to do the subtraction. It doesn't have to be that way. Suppose instead that you had the data arranged as I have suggested, with the 2D arrays the first two dimensions. Then the above could be accomplished without the squeeze as follows:
>
> > image_2d=Data(:,:,gel_Counter) - water_Average;
>
> > That looks cleaner and is easier to read. It is also more efficient for MATLAB to handle. In fact, thinking in a more MATLAB vectorized fashion, you could even do the whole subtraction for all the 2D arrays at once:
>
> > images = bsxfun(@minus, Data, water_Average);
>
> > The images variable will be a 3D array containing the 2D slices with water_Average subtracted out. Then you can pick off the resulting 2D arrays and work with them.
>
> > James Tursa- Hide quoted text -
>
> - Show quoted text -

Is there any advantage to storing the images in a cell of matrices? I
sometimes do this when I'm changing the size of a matrix, but is there
any reason to do it in general?

Subject: Storing multiple 2D arrays in a 3D array

From: James Tursa

Date: 4 Nov, 2010 13:53:03

Message: 7 of 8

JC <cardani@gmail.com> wrote in message <550a0346-5674-4901-b6dc-a743bfcc3da1@z19g2000yqb.googlegroups.com>...
>
> Is there any advantage to storing the images in a cell of matrices? I
> sometimes do this when I'm changing the size of a matrix, but is there
> any reason to do it in general?

It completely depends on what you are doing with the images downstream in your code. If you are doing operations that can be vectorized, then a 3D arrangement makes sense. But if you are pulling out the 2D slices to work with them individually, then a cell array makes sense.

James Tursa

Subject: Storing multiple 2D arrays in a 3D array

From: Sean

Date: 4 Nov, 2010 13:58:04

Message: 8 of 8

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <iaudrv$aoi$1@fred.mathworks.com>...
> JC <cardani@gmail.com> wrote in message <550a0346-5674-4901-b6dc-a743bfcc3da1@z19g2000yqb.googlegroups.com>...
> >
> > Is there any advantage to storing the images in a cell of matrices? I
> > sometimes do this when I'm changing the size of a matrix, but is there
> > any reason to do it in general?
>
> It completely depends on what you are doing with the images downstream in your code. If you are doing operations that can be vectorized, then a 3D arrangement makes sense. But if you are pulling out the 2D slices to work with them individually, then a cell array makes sense.
>
> James Tursa

Cell array also makes sense if the images aren't all the same size. If that's the case you would have to zero-pad them to fit together in a 3D array...

Tags for this Thread

No tags are associated with 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