MATLAB Answers

How can I splitt up a big matrix into smaller matrises, splitting it up by values on the x-axis

조회 수: 1(최근 30일)
FV
FV 2020년 2월 17일
댓글: Adam Danz 2020년 2월 18일
I have a large matrix that should be divided into smaller matrices based on the value in the x-axis. The large matrix, 100X3, has values ​​where the columns represent the x, y and z-axis.
The large matrix should be divided into 10 smaller matrices, the first one containing values x<50​​, the next one x-values from 50-59, next one 60-69 and so on .
There is no correlation in how many rows there are of each x-value, which can make the first matrix a 50x3 matrix and the next one a 7x3 matrix.
It all have to be done using for loop, and hopfully be made in a way that makes it easy to change, both the number of matrices and the value they are ranging between.
I have figured out how i can divide it into smaller matrices, but just by dividing it into equal parts.
Actually i work with a huge point cloud of a bridge and it have to be divided into smaller segments so that it is easier to work with.
I hope someone can help me..
  댓글 수: 4

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

채택된 답변

Joseph Cheng
Joseph Cheng 2020년 2월 18일
편집: Joseph Cheng 2020년 2월 18일
so the main question is whether the ranges for the smaller matrixes are known or need to be detectable. then you can use a for loop to break them into smaller matrixes
%gen dummy data
A(:,1)=1:100;
A(:,2:3)=randi(100,100,2);
%designate start and stop ranges for x
xRngstart=[1 50 60 70 80 90 100]
xRngstop =[xRngstart(2:end)-1 xRngstart(end)];
%loop through each start index and save it in a structure
for ind = 1:numel(xRngstart)
Processed(ind).SmallerM = A(xRngstart(ind):xRngstop(ind),:);
end
or if x is not really integers you can do the same thing with ranges checks
%gen dummy data
A(:,1)=0:100;
A(:,2:3)=randi(100,numel(A(:,1)),2);
%basically the ranges of the smaller blocks are designated by being between n and n+1
xRngs=[min(A(:,1)) 50 60 70 80 90 100 max(A(:,1))]
for ind = 1:numel(xRngs)-1
rows = A(:,1)>=xRngs(ind) & A(:,1)<xRngs(ind+1); %check to see which rows of A are between
%xRng(ind) and xRng(ind+1)
Processed(ind).SmallerM = A(rows,:);
end
  댓글 수: 3

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

추가 답변(1개)

Jakob B. Nielsen
Jakob B. Nielsen 2020년 2월 18일
편집: Jakob B. Nielsen 2020년 2월 18일
Logical indexing will help you here - a for loop is also possible but would be much more complicated.
Lets call your initial matrix X.
A=X(X(:,1)<50,:);
This places into A all values of X, where the condition in column 1 meets the requirement - here less than 50 - that you specify. When there are multiple conditions, an & sign means logic AND (takes the value if and only if both conditions are true), and a | sign means logic OR (takes the value if one or the other or both are true).
B=X(X(:,1)>=50 & X(:,1)<60,:); %greater than or equal 50, but less than 60.
Of course, remember that if you put less than 50 as the first condition, then the next condition must be greater or equal to 50; if you only put greater than 50, you will miss out all values which are exactly 50.

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by