How to make an X by 3 matrix into a grid of points

So currently I have a 262014x3 matrix that has 262014 coordinates of an image.
To be able to use this for my application it has to be a 3D matrix of binary points. So I want to put a point in a, let's say, 512x512x512 matrix (I know, I'll lose a lot of definition, that's not the concern), how would I go about that?
Just a side note, the initial matrix was made by using stlread (as found here http://www.mathworks.com/matlabcentral/fileexchange/29906-binary-stl-file-reader) And I'm only using the coordinates of the vertices.

 Accepted Answer

Try this:
m3d = false(512,512,512); % Initialize binary image.
for row = 1 : size(coordinates, 1)
row = coordinates(row, 1);
col = coordinates(row, 2);
slice = coordinates(row, 3);
m3d(row, col, slice) = true;
end

7 Comments

Hmm, I don't know if that will work or not because I keep getting the Out of Memory error and here are my system's memory specs
>> [uV sV] = memory
uV =
MaxPossibleArrayBytes: 1.1862e+10
MemAvailableAllArrays: 1.1862e+10
MemUsedMATLAB: 875868160
sV =
VirtualAddressSpace: [1x1 struct]
SystemMemory: [1x1 struct]
PhysicalMemory: [1x1 struct]
>> sV.SystemMemory
ans =
Available: 1.1862e+10
>> sV.PhysicalMemory
ans =
Available: 4.5250e+09
Total: 8.4854e+09
end
So I don't know where to go from here. Thanks for the tip though!
Worked fine for me. Please copy and paste the code below exactly as it appears and paste it into a blank editor window and run it. Then tell me if it works or not. Like I said, it works fine for me and runs pretty fast.
coordinates = randi(512, 262014, 3);
m3d = false(512,512,512); % Initialize binary image.
for row = 1 : size(coordinates, 1)
row = coordinates(row, 1);
col = coordinates(row, 2);
slice = coordinates(row, 3);
m3d(row, col, slice) = true;
end
msgbox('Done with demo!');
Ahh, I found the problem. So that generated number from 0-512 and the numbers I'm dealing with have 5 digits of precision and if you increase the 512 in randi to something larger that's where the error came from.
If your numbers are floating point numbers, then you have to round them to the nearest integer if you are going to use them as an index. So it doesn't matter if the value is 145.1234563532432 or simply 145.1 - it doesn't matter how many digits of precision you have . You can't have any - it must be an integer if it's to be an index. But it looks like you now have integers in the range 1-512 (through rounding or scaling or whatever). So once you have that, you can use the code I gave. If that solves the problem, then can you please mark the answer as accepted?
So you don't have sufficient memory to solve your problem. Solve smaller problems, or get more memory.
Well the thing is the points from readSTL were from 93.137-98.463 for x, 121.354-127.352 for y, and 45.362-49.234 for z. So I just multiplied them by 1000 since scale isn't a problem, but the precision is still relevant and going down to 3 digits obscures the image too much.
And yeah, I guess I could get more memory, I was just trying to see if there was a less memory intensive answer available, but no worries.
Thanks for the help!
I'm confused. Subtract the min, then multiply by 511/(max - min), add 1. This will scale it to 1-512. Then round.
If you are truly memory limited, then use a bit smaller matrix.
If you really just want to visualize the points as a scatter cloud, then why not use plot3 in one call? If your goal is really to visualize this as an object, then use an alpha shape, or a convex hull if the set is convex.

Sign in to comment.

Categories

Tags

Asked:

on 22 Jun 2014

Answered:

DGM
on 13 Jul 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!