MATLAB: Read each line of 3 values of a ( n*3 matrix) as a x,y,z positions

read lines as positions

Hi there! Hi have this k =
1 1 2
2 1 2
2 2 2
n1 n2 n3
n4 n5 n6
n7 n8 n9
n11 n12 n13
(...) (...) (...)
And I want for each line to be read as a position xyz, .
For example:
1st line: x=1, y=1, z=2
Afterwards I want to create a new 2*2*2 matrix of zeros, where the xyz positions that I got above are written with a 1 on the new matrix.
ANy help? I need to solve these asap!
Regards

Best Answer

Try this:
k =[...
1 1 1
2 1 2
2 2 2]
x = k(:, 1);
y = k(:, 2);
z = k(:, 3);
newMatrix = zeros(2,2,2);
for j = 1 : size(k, 1)
% Note: y = row, x = column.
newMatrix(y(j), x(j) ,z(j)) = 1;
end
newMatrix % Print to command window.