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

1 view (last 30 days)
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
  1 Comment
André Sousa
André Sousa on 26 May 2014
following the same question: This is my full code:
aa=rand(2,2,2)
aa(1,1,1)=5;
aa(2,1,1)=8;
aa(1,2,1)=3;
aa(2,2,1)=3;
aa(1,1,2)=7;
aa(2,1,2)=8; aa(1,2,2)=3;
aa(2,2,2)=3;
dimention_aa=size(aa); aareshaped=reshape(aa,[2,4])
ii=rand(3,1);
ii(1,1)=8;
ii(2,1)=3;
ii(3,1)=7;
for( l=(ii(1):ii(end)))
ind_reshaped=find(aareshaped==l)
[R1, C1, S1]=ind2sub(dimention_aa,find(aareshaped==l)
k=[R1, C1, S1]
x1=k(:,1)
y1=k(:,2)
z1=k(:,3)
newMatrix = zeros(dimention_aa)
for j = 1 : size(k, 1)
newMatrix( x1(j) , y1(j) ,z1(j)) = 1
end
% roi_mat(l,:)=x;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%% Basically the k matrix(that has lines correspondent to xyz positions) is genereated by
[R1, C1, S1]=ind2sub(dimention_aa,find(aareshaped==l)
k=[R1, C1, S1]
And l is a vector with diferent numbers. How do I do the same procedure to go through every element of the l? The code I wrote above, doesn't work quite well.. Regards

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 26 May 2014
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.

More Answers (1)

André Sousa
André Sousa on 26 May 2014
Thanks for the quick answer!

Community Treasure Hunt

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

Start Hunting!