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:
Collect indices and to insert the zeros

Subject: Collect indices and to insert the zeros

From: John

Date: 20 Mar, 2011 05:21:04

Message: 1 of 10

Hello All,
I have got a slightly tricky Matlab question that I am hoping to receive some help from you guys.

I have got got a data sample (34361x4 data array with x, y and z cooridinates). I am going to use a simplified version as an exmaple. Let's say the data sample looks like this:
   Z Y X
1 3 1 1
2 4 1 1
3 2 2 1
4 3 2 1

The first column is the index. From the second to the fourth column (Z, Y & X), each column in this data sample is supposed to be reiterate from "1 to 5".

What I am hoping to achieve is this:

I would like to ask the computer to go through the second column (Z column) for y=1 & x=1. If there is a number missing (i.e in this case, 1,2,5 are missing for y=1 & x=1), I would like to add "0s" in the rows that they are missing. Ideally, I am hoping the index column will automatically update itself. But this is not essential.

I would also like to repeat this process for y=2 & x=1 all the way to y=5 & x=5

This is what I am hoping to achieve:

   Z Y X
1 0 0 0 (missing 1)
2 0 0 0 (missing 2)
3 3 1 1
4 4 1 1
5 0 0 0 (missing 5)
6 0 0 0 (missing 1)
7 2 2 1
8 3 2 1
9 0 0 0 (missing 4)
10 0 0 0 (missing 5)

I hope this is not too confusing. If you have need any clarifications, please feel free to e-mail me weicongyi@gmail.com

Thanks a lot in advance!

I am looking forward to hear everyone's suggestion.
 
John

Subject: Collect indices and to insert the zeros

From: Sean de

Date: 20 Mar, 2011 07:04:04

Message: 2 of 10

"John" wrote in message <im42s0$502$1@fred.mathworks.com>...
> Hello All,
> I have got a slightly tricky Matlab question that I am hoping to receive some help from you guys.
>
> I have got got a data sample (34361x4 data array with x, y and z cooridinates). I am going to use a simplified version as an exmaple. Let's say the data sample looks like this:
> Z Y X
> 1 3 1 1
> 2 4 1 1
> 3 2 2 1
> 4 3 2 1
>
> The first column is the index. From the second to the fourth column (Z, Y & X), each column in this data sample is supposed to be reiterate from "1 to 5".
>
> What I am hoping to achieve is this:
>
> I would like to ask the computer to go through the second column (Z column) for y=1 & x=1. If there is a number missing (i.e in this case, 1,2,5 are missing for y=1 & x=1), I would like to add "0s" in the rows that they are missing. Ideally, I am hoping the index column will automatically update itself. But this is not essential.
>
> I would also like to repeat this process for y=2 & x=1 all the way to y=5 & x=5
>
> This is what I am hoping to achieve:
>
> Z Y X
> 1 0 0 0 (missing 1)
> 2 0 0 0 (missing 2)
> 3 3 1 1
> 4 4 1 1
> 5 0 0 0 (missing 5)
> 6 0 0 0 (missing 1)
> 7 2 2 1
> 8 3 2 1
> 9 0 0 0 (missing 4)
> 10 0 0 0 (missing 5)
>
> I hope this is not too confusing. If you have need any clarifications, please feel free to e-mail me weicongyi@gmail.com
>
> Thanks a lot in advance!
>
> I am looking forward to hear everyone's suggestion.
>
> John

Is this what you want?


data = [
3 1 1
4 1 1
2 2 1
3 2 1];


[xx yy zz] = ndgrid(1:5);
data2 = zeros(numel(xx),3);

IDXgrid = [zz(:),yy(:),xx(:)];
IDXgrid = sortrows(IDXgrid,[3 2 1]);

[idxR] = ismember(IDXgrid,data(:,[1 3 2]),'rows');
data2(idxR,:) = data;

%%%
I think you left a few rows out in your example. I left the first column (row index) out in mine though it would be easy to add. Anyway, this type of computation will require a lot of memory for a small problem and is generally recommended against. What are you trying to accomplish with this? Perhaps we can find a better solution.

Subject: Collect indices and to insert the zeros

From: Bruno Luong

Date: 20 Mar, 2011 08:19:04

Message: 3 of 10

A = [1 3 1 1;
    2 4 1 1;
    3 2 2 1
    4 3 2 1 ];

xy = A(:,3:4);
xyu = unique(xy,'rows')
z = A(:,2);
nz = 5;

[trash, ixy] = ismember(xy,xyu,'rows')
[trash, iz] = ismember(z,1:nz)

B = zeros(nz*length(xyu),4);
i = sub2ind([nz,length(xyu)], iz, ixy);
B(i,:) = A;
B(:,1) = 1:size(B,1)

Bruno

Subject: Collect indices and to insert the zeros

From: John

Date: 20 Mar, 2011 11:17:04

Message: 4 of 10

"Sean de " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <im48t4$4ti$1@fred.mathworks.com>...
> "John" wrote in message <im42s0$502$1@fred.mathworks.com>...
> > Hello All,
> > I have got a slightly tricky Matlab question that I am hoping to receive some help from you guys.
> >
> > I have got got a data sample (34361x4 data array with x, y and z cooridinates). I am going to use a simplified version as an exmaple. Let's say the data sample looks like this:
> > Z Y X
> > 1 3 1 1
> > 2 4 1 1
> > 3 2 2 1
> > 4 3 2 1
> >
> > The first column is the index. From the second to the fourth column (Z, Y & X), each column in this data sample is supposed to be reiterate from "1 to 5".
> >
> > What I am hoping to achieve is this:
> >
> > I would like to ask the computer to go through the second column (Z column) for y=1 & x=1. If there is a number missing (i.e in this case, 1,2,5 are missing for y=1 & x=1), I would like to add "0s" in the rows that they are missing. Ideally, I am hoping the index column will automatically update itself. But this is not essential.
> >
> > I would also like to repeat this process for y=2 & x=1 all the way to y=5 & x=5
> >
> > This is what I am hoping to achieve:
> >
> > Z Y X
> > 1 0 0 0 (missing 1)
> > 2 0 0 0 (missing 2)
> > 3 3 1 1
> > 4 4 1 1
> > 5 0 0 0 (missing 5)
> > 6 0 0 0 (missing 1)
> > 7 2 2 1
> > 8 3 2 1
> > 9 0 0 0 (missing 4)
> > 10 0 0 0 (missing 5)
> >
> > I hope this is not too confusing. If you have need any clarifications, please feel free to e-mail me weicongyi@gmail.com
> >
> > Thanks a lot in advance!
> >
> > I am looking forward to hear everyone's suggestion.
> >
> > John
>
> Is this what you want?
>
>
> data = [
> 3 1 1
> 4 1 1
> 2 2 1
> 3 2 1];
>
>
> [xx yy zz] = ndgrid(1:5);
> data2 = zeros(numel(xx),3);
>
> IDXgrid = [zz(:),yy(:),xx(:)];
> IDXgrid = sortrows(IDXgrid,[3 2 1]);
>
> [idxR] = ismember(IDXgrid,data(:,[1 3 2]),'rows');
> data2(idxR,:) = data;
>
> %%%
> I think you left a few rows out in your example. I left the first column (row index) out in mine though it would be easy to add. Anyway, this type of computation will require a lot of memory for a small problem and is generally recommended against. What are you trying to accomplish with this? Perhaps we can find a better solution.

%%%%%%
Hi Sean and everyone else outhere,
Thank you so much for your reply. It is greatly appreciated.

After reading your comments, I thought I will let you know the full story if you do not mind. Also, I am hoping to seek any suggestions on how to solve this problem more efficiently.

Basically, these "Z, Y & X" coordinates (2nd, 3rd & 4th Column) are ultrasound scanned data of a cubic bone model with the dimension of 143x143x143 voxels in each direction. (voxels can be interpreted as "data points). If a bone voxel is detected, the cooridinate of this bone voxel is recorded. The rest of "missing" voxels that I am hoping to recover are marrow voxels.

What I am trying to do is to recover those "missing" marrow voxels first. I will explain the reason shortly.

Following that, I need to conduct a "resolution analysis". Basically, instead of treating the cubic bone model as 143x143x143 (it is called "resolution 1x1" analysis), I need to treat the cubic bone model as 71x71x71 (71 as 143/2, it is called "resolution 2x2" analysis), an then, I will need to do 35x35x35 (resolution 4x4 analysis) until 1x1x1 (resolution 128x128 analysis).

To be able to conduct "resolution analysis", I need to know the exact location of each bone voxel and marrow voxel. In other words, I need to know in any given location within the 143x143x143 cubic bone model, whether the voxel at this location is a bone (represented as in 1) voxel or a marrow (represented as in 0) voxel.

Let's use the resolution "2x2" analysis as an example. Once I can tell in any given location, the voxel in this location is bone or marrow, I can then work out in a "2x2" square box, what is this "transit time" of this voxel is going to be using the following formula.

Eg: If in this 2x2 square box, I have got 1100. The transit time is then going to be 0.5. 1111, the transit time is going to be 1 etc.

Anyway, the main objective that I would like to achieve is to be able to know in any given location of the cubic bone model, the voxel in this location is a bone (represented as in 1) voxel or a marrow (represented as in 0) voxel. Basically, convert this cubic model into 0s and 1s.

I hope I have explained clearly. Thank you so much for reading this. If you could kindly make some suggestions, that would be greatly appreciated.

Thanks a lot in advance.

John

Subject: Collect indices and to insert the zeros

From: John

Date: 20 Mar, 2011 11:51:03

Message: 5 of 10

%%%%%%%
Just to tidy up my previous reply to Sean:

> Hi Sean and everyone else outhere,
> Thank you so much for your reply. It is greatly appreciated.
>
> After reading your comments, I thought I will let you know the full story if you do not mind. Also, I am hoping to seek any suggestions on how to solve this problem more efficiently.
>
> Basically, these "Z, Y & X" coordinates (2nd, 3rd & 4th Column) are ultrasound scanned data of a cubic bone model with the dimension of 143x143x143 voxels in each direction. (voxels can be interpreted as "data points). If a bone voxel is detected, the cooridinate of this bone voxel is recorded. The rest of "missing" voxels that I am hoping to recover are marrow voxels.
>
> What I am trying to do is to recover those "missing" marrow voxels first. I will explain the reason shortly.
>
> Following that, I need to conduct a "resolution analysis". Basically, instead of treating the cubic bone model as 143x143x143 (it is called "resolution 1x1" analysis), I need to treat the cubic bone model as 71x71x71 (71 as 143/2, it is called "resolution 2x2" analysis), an then, I will need to do 35x35x35 (resolution 4x4 analysis) until 1x1x1 (resolution 128x128 analysis).
>
> To be able to conduct "resolution analysis", I need to know the exact location of each bone voxel and marrow voxel. In other words, I need to know in any given location within the 143x143x143 cubic bone model, whether the voxel at this location is a bone (represented as in 1) voxel or a marrow (represented as in 0) voxel.
>
> Let's use the resolution "2x2" analysis as an example. Once I can tell in any given location, the voxel in this location is bone or marrow, I can then work out in a "2x2" square box, what is this "transit time" of this voxel is going to be using the following formula.
>
> Eg: If in this 2x2 square box, I have got 1100. The transit time is then going to be 0.5. 1111, the transit time is going to be 1 etc.
>
> Anyway, the main objective that I would like to achieve is to be able to know in any given location of the cubic bone model, the voxel in this location is a bone (represented as in 1) voxel or a marrow (represented as in 0) voxel. Basically, convert this cubic model into 0s and 1s.
>
> I hope I have explained clearly. Thank you so much for reading this. If you could kindly make some suggestions, that would be greatly appreciated.
>
> Thanks a lot in advance.
>
> John

Subject: Collect indices and to insert the zeros

From: Sean de

Date: 20 Mar, 2011 17:26:05

Message: 6 of 10

"John" wrote in message <im4pn7$ko3$1@fred.mathworks.com>...
> %%%%%%%
> Just to tidy up my previous reply to Sean:
>
> > Hi Sean and everyone else outhere,
> > Thank you so much for your reply. It is greatly appreciated.
> >
> > After reading your comments, I thought I will let you know the full story if you do not mind. Also, I am hoping to seek any suggestions on how to solve this problem more efficiently.
> >
> > Basically, these "Z, Y & X" coordinates (2nd, 3rd & 4th Column) are ultrasound scanned data of a cubic bone model with the dimension of 143x143x143 voxels in each direction. (voxels can be interpreted as "data points). If a bone voxel is detected, the cooridinate of this bone voxel is recorded. The rest of "missing" voxels that I am hoping to recover are marrow voxels.
> >
> > What I am trying to do is to recover those "missing" marrow voxels first. I will explain the reason shortly.
> >
> > Following that, I need to conduct a "resolution analysis". Basically, instead of treating the cubic bone model as 143x143x143 (it is called "resolution 1x1" analysis), I need to treat the cubic bone model as 71x71x71 (71 as 143/2, it is called "resolution 2x2" analysis), an then, I will need to do 35x35x35 (resolution 4x4 analysis) until 1x1x1 (resolution 128x128 analysis).
> >
> > To be able to conduct "resolution analysis", I need to know the exact location of each bone voxel and marrow voxel. In other words, I need to know in any given location within the 143x143x143 cubic bone model, whether the voxel at this location is a bone (represented as in 1) voxel or a marrow (represented as in 0) voxel.
> >
> > Let's use the resolution "2x2" analysis as an example. Once I can tell in any given location, the voxel in this location is bone or marrow, I can then work out in a "2x2" square box, what is this "transit time" of this voxel is going to be using the following formula.
> >
> > Eg: If in this 2x2 square box, I have got 1100. The transit time is then going to be 0.5. 1111, the transit time is going to be 1 etc.
> >
> > Anyway, the main objective that I would like to achieve is to be able to know in any given location of the cubic bone model, the voxel in this location is a bone (represented as in 1) voxel or a marrow (represented as in 0) voxel. Basically, convert this cubic model into 0s and 1s.
> >
> > I hope I have explained clearly. Thank you so much for reading this. If you could kindly make some suggestions, that would be greatly appreciated.
> >
> > Thanks a lot in advance.
> >
> > John

Excellent reply, John, we rarely get that detailed a response.

To acquire the indices the way you are doing it:

bytes = (1x4 (points/row))*every_voxel*8bytes/voxel
which is a lot of bytes when you figure that each voxel in a 3d logical volume is a byte.

So when thinking in 3D volumes we'll use row/col/page i.e. Y/X/Z.

B = false(143,143,143); %marrow voxels
B(sub2ind(size(B),data(:,2),data(:,3),data(:,1))) = true; %bone voxels

Now you can use typical functions to do your calculations on this volume or generate information as you need it.

Good Luck!

-Sean

Subject: Collect indices and to insert the zeros

From: John

Date: 23 Mar, 2011 01:06:05

Message: 7 of 10

Hello Sean and everyone else outhere,

Thank you very much for all your help.

I have followed Sean's suggestion and I have succuessfully converted each voxel into "0s" (marrow) and "1s" (bone).

Unfortunately, I have got stuck again with my next task. I am hoping someone could kindly help me out here.

Now, the following task is to conduct the so called "resolution analysis". Basically, the original size of the cubic model was 143x143x143 voxels. What I need to do now is to treat the cubic bone model as 71x71x71 (71 as 143/2, it is called "resolution 2x2" analysis), an then, I will need to do 35x35x35 (resolution 4x4 analysis) until 1x1x1 (resolution 128x128 analysis).

Let's use the resolution "2x2" analysis as an example. I know now in given location, whether the voxel in this location is bone or marrow. The next task is to workout in a "2x2" sqaure box (let's called it super-voxel), what is the "transit time" of this super-voxel is going to be using the following formula.

If in this 2x2 square box, I have got 1100. The transit time is then going to be 0.5.
1111, the transit time is going to be 1
0000, the transit time is going to be 0
1110, the transit time is going to be 0.75 (the order of 1s and 0s do not matter)
0001, the transit time is going to be 0.25 (the order of 1s and 0s do not matter)

What I am hoping to do is to write a code that can do the following,
it starts to check "row 1, column 1; row 1, column 2; row 2, column 1; row 2, column 2" and treat this four voxels (data points) as a the "super-voxel" and workout the value according to the formula mentioned previously. The transit time value then is stored into a matrix with the size of 71x71x71. Then it will move on to check "row 1, column 3; row 1, column 4; row 2, column3; row 2, column 4" and stored the transit time value into the 71x71x71 matrix.

Any suggestions on solving this matter is greatly appreciated.

Thanks a lot in advance.

John

Subject: Collect indices and to insert the zeros

From: Sean de

Date: 23 Mar, 2011 15:14:20

Message: 8 of 10

"John" wrote in message <imbh1t$341$1@fred.mathworks.com>...
> Hello Sean and everyone else outhere,
>
> Thank you very much for all your help.
>
> I have followed Sean's suggestion and I have succuessfully converted each voxel into "0s" (marrow) and "1s" (bone).
>
> Unfortunately, I have got stuck again with my next task. I am hoping someone could kindly help me out here.
>
> Now, the following task is to conduct the so called "resolution analysis". Basically, the original size of the cubic model was 143x143x143 voxels. What I need to do now is to treat the cubic bone model as 71x71x71 (71 as 143/2, it is called "resolution 2x2" analysis), an then, I will need to do 35x35x35 (resolution 4x4 analysis) until 1x1x1 (resolution 128x128 analysis).
>
> Let's use the resolution "2x2" analysis as an example. I know now in given location, whether the voxel in this location is bone or marrow. The next task is to workout in a "2x2" sqaure box (let's called it super-voxel), what is the "transit time" of this super-voxel is going to be using the following formula.
>
> If in this 2x2 square box, I have got 1100. The transit time is then going to be 0.5.
> 1111, the transit time is going to be 1
> 0000, the transit time is going to be 0
> 1110, the transit time is going to be 0.75 (the order of 1s and 0s do not matter)
> 0001, the transit time is going to be 0.25 (the order of 1s and 0s do not matter)
>

A 2x2 analysis on each plane of your 3d volume (in which case you'd have to skip every other plane)? Or do you want 2x2x2 as a whole chunk?

For now look at the functions:
conv2
convn

Basically you'll want to convolve your volume, let's call it V, with a ones matrix of size 2x2. For a 2-d slice:
conv2(V(:,:,1),ones(2)./4);
This will multiply each 2x2 box with ones and divide by 4, so if there are 3 ones, 1 zero: (3*1)/4=0.75. You'll have to decide what to do at your boundary conditions. Do you want to zero pad them for the calculation? Do you want to throw away the boundary voxels?

Subject: Collect indices and to insert the zeros

From: John

Date: 27 Mar, 2011 00:04:07

Message: 9 of 10

A 2x2 analysis on each plane of your 3d volume (in which case you'd have to skip every other plane)? Or do you want 2x2x2 as a whole chunk?
>
> For now look at the functions:
> conv2
> convn
>
> Basically you'll want to convolve your volume, let's call it V, with a ones matrix of size 2x2. For a 2-d slice:
> conv2(V(:,:,1),ones(2)./4);
> This will multiply each 2x2 box with ones and divide by 4, so if there are 3 ones, 1 zero: (3*1)/4=0.75. You'll have to decide what to do at your boundary conditions. Do you want to zero pad them for the calculation? Do you want to throw away the boundary voxels?

%%%
Hello Sean,

Once again, thank you very much for help on this matter!

To answer your first question:

Step 1: The first step that I would like to do is to determine the "overall" transit time of each "2x2" square box (slab) using the formula that I have mentioned in my previous post along the Z axis (from Z=1 to Z=142). The formulas are "0000=1","0001 = 0.25", "0011 = 0.5", "0111= 0.75" and "1111 = 1"

Step 2: Once I have done that, I would then like to determine the "final" transit time of this "2x2x142" chunck.

Step3: Once I have done this, I would like to move on to the next "2x2x142" chunck to repeat the same process. It does not matter whether I have moved on to the one next to the "2x2x142" chunck that I have done or the one below. However, what is your suggestions on once I have reached to the boundary, can I ask the computer to move a level down or to the next available line to repeat (step 1, 2 & 3)?

I am not 100% sure about what you meant " you'll have to decide what to do at your boundary conditions. Do you want to zero pad them for the calculation? Do you want to throw away the boundary voxels?". Are you talking about the fact that 143/2 does not equal to a whole number? Would it possible just to treat the size of the cubic as "142x142x142"? And every single time when the size of the voxel cannot be divided by 2, I will just do (the size number -1) in order to be able to be divided by 2?

Once again, any suggestions would be greatly appreciated.

Thanks a lot in advance.

John

Subject: Collect indices and to insert the zeros

From: John

Date: 27 Mar, 2011 01:00:21

Message: 10 of 10

A 2x2 analysis on each plane of your 3d volume (in which case you'd have to skip every other plane)? Or do you want 2x2x2 as a whole chunk?
>
> For now look at the functions:
> conv2
> convn
>
> Basically you'll want to convolve your volume, let's call it V, with a ones matrix of size 2x2. For a 2-d slice:
> conv2(V(:,:,1),ones(2)./4);
> This will multiply each 2x2 box with ones and divide by 4, so if there are 3 ones, 1 zero: (3*1)/4=0.75. You'll have to decide what to do at your boundary conditions. Do you want to zero pad them for the calculation? Do you want to throw away the boundary voxels?

%%%
Hello Sean and everyone outhere,

Sorry to bother you all again.

I would just like to tidy up my previous post a bit.

For the resolution analysis, these steps are what I am hoping to achieve.

Step 1: The first step that I would like to do is to determine the "overall" transit time of each "2x2" square box (slab) using the formula that I have mentioned in my previous post along the Z axis (from Z=1 to Z=142). The formulas are "0000=1","0001 = 0.25", "0011 = 0.5", "0111= 0.75" and "1111 = 1"

Step 2: Once I have done that, I would then like to determine the "final" transit time of this "2x2x142" chunck by adding each "overall" transit time that I have calculated in step 1 together.

Step3: Once I have done this, I would like to move on to the next "2x2x142" chunck to repeat the same process. It does not matter whether I have moved on to the one next to the "2x2x142" chunck that I have done or the one below. Eventually, I am hoping to have "71x71 = 5041" numbers of transit time in the new matrix.

By the way, what is your suggestions on once I have reached to the boundary, can I ask the computer to move a level down or to the next available line to repeat (step 1, 2 & 3)?

I am not 100% sure about what you meant " you'll have to decide what to do at your boundary conditions. Do you want to zero pad them for the calculation? Do you want to throw away the boundary voxels?". Are you talking about the fact that 143/2 does not equal to a whole number? Would it possible just to treat the size of the cubic as "142x142x142"? And every single time when the size of the voxel cannot be divided by 2, I will just do (the size number -1) in order to be able to be divided by 2?

Once again, any suggestions would be greatly appreciated.

Thanks a lot in advance.

John

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