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

조회 수: 1 (최근 30일)
André Sousa
André Sousa 2014년 5월 26일
댓글: André Sousa 2014년 5월 26일
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
André Sousa
André Sousa 2014년 5월 26일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2014년 5월 26일
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.

추가 답변 (1개)

André Sousa
André Sousa 2014년 5월 26일
Thanks for the quick answer!

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by