필터 지우기
필터 지우기

extend a Matrix in every iteration

조회 수: 2 (최근 30일)
Fernando Arias
Fernando Arias 2018년 5월 18일
편집: jonas 2018년 5월 18일
Hi, I'm having some problems with my code. I have a 3 dimension matrix in which index represent a coordinate, for example, a 10x10x10 array where the first index are 1, 1, 1 represent the point x=1, y=1 and z=1. And the value in the array represent the number of points in theese coordinates. Now I want to use ptCloud function, so I have to extract all the points from my array. What I'm trying is this (If I were in the case 10*10*10 matrix A):
MAT=[]
for i=1:1000
[Yj Xj Zj]=ind2sub(size(A), i);
value=A(Yj Xj Zj);
P=[Yj Xj Zj];
P2=repmat(P,value,1)
MAT=[MAT;P2]
end
With this code I already have a matrix MAT whith every points. But the case is that my array is 200x200x160, and I noticed that in every iteration the time that the code requires increases a lot, so I think there would be something wrong in the for loop.
Thanks so much.
  댓글 수: 3
Fernando Arias
Fernando Arias 2018년 5월 18일
MAT is the matrix wich contains all points, for example, if there is a 3 in A(1,1,1) means that I have three points in (1,1,1) so I add 3 rows of [1, 1, 1] to the matrix MAT. Finally I would like to have a matrix with all points where first colum is x coordinate, seconx y and third z.
I think there would be another way quiker, becouse with this code I can't have the matrix in more than a day.
jonas
jonas 2018년 5월 18일
편집: jonas 2018년 5월 18일
EDIT: nvm, Mr. Cobeldick solved that beautifully

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

채택된 답변

Stephen23
Stephen23 2018년 5월 18일
편집: Stephen23 2018년 5월 18일
A = randi(9,10,10,10);
[X,Y,Z] = ind2sub(size(A),1:numel(A));
mat = repelem([X(:),Y(:),Z(:)],A(:),1,1);
but I don't see much point in generating subscript indices: it would be easier to define and use linear indices:
idx = repelem(1:numel(A),A(:).')

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by