Create a structure from discrete column vectors or a 3 x n matrix composed of column vectors
조회 수: 9 (최근 30일)
이전 댓글 표시
Hi
I'm trying to build a structure consisting of elements which are column vectors representing x, y, z points. So far, I have written the following code, which indexes arrays representing x, y, z positions of points from a structure called 'data' (derived from a .ply 3d graphics file) and rearranges them into a 3 x n matrix:
% orders vertex data from structure into column vectors
x = data.vertex.x
y = data.vertex.y
z = data.vertex.z
% vertically concatenates x, y, z to form a 3 x n matrix stored in the
% structure, verts
verts = horzcat(x, y, z)
%Sequentially indexes PX PY PZ into a 3 x n matrix where columns are x y z
%components of position vectors
for i=1:length(x)
vertsnew(:,i)=[x(i);y(i);z(i)];
end
This creates a matrix of the form:
3.7427 3.74984 1.38903
4.7384 3.76483 8.76289
9.6389 9.62843 5.78290 .............
I want to create a structure where each column vector in this array is a seperate element. I thought the following might do the trick:
vertz=struct();
for k=1:size(x)
Vk = [vertsnew(1,k)];
vertz.(['VZ' num2str(k)])=Vk;
end
But this results in a structure with x, y, z arranged sequentially as individual elements. E.g:
vertz.VZ.1 = 3.7427
vertz.VZ.2 = 4.7384
vertz.VZ.3 = 9.6389
I am able to create individual column vectors using:
%Orders arrays, into discrete column vectors
for i=1:length(x)
assignin('base',['vertv' num2str(i)],[x(i);y(i);z(i)])
end
E.g.
vertv1 = 3.7427
vertv2 = 4.7384
etc
Though I am unsure how to obtain the desired structure from these seperate arrays. Any help would be greatly appreciated.
Thomas
댓글 수: 6
Matt J
2012년 11월 27일
편집: Matt J
2012년 11월 27일
If you're talking about a 4x4 rototranslation matrix, then you would want to pad along the bottom with 1s instead of 0s. Instead of using a 4x4 matrix, a more efficient way to perform a rototranslation (probably) is
out = bsxfun(@plus,R*vertsnew,t)
because this way MATLAB doesn't have to copy all of the data in vertsnew to a new 4xn array. That would matter mainly if n is large.
채택된 답변
Azzi Abdelmalek
2012년 11월 27일
편집: Azzi Abdelmalek
2012년 11월 27일
y=[3.7427 3.74984 1.38903
4.7384 3.76483 8.76289
9.6389 9.62843 5.78290]
for k=1:size(y,2)
vertz.(sprintf('VZ%d',k))=y(:,k)
end
댓글 수: 2
Azzi Abdelmalek
2012년 11월 27일
I agree with him that is not the best way, but at the end you are the only person to decide how to do it. Also, whatever you do, there is always a better way!
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!