MATLAB Answers

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

6 views (last 30 days)
FV
FV on 17 Feb 2020
Commented: Adam Danz on 18 Feb 2020
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 Comments

Show 1 older comment

Sign in to comment.

Accepted Answer

Joseph Cheng
Joseph Cheng on 18 Feb 2020
Edited: Joseph Cheng on 18 Feb 2020
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 Comments

FV
FV on 18 Feb 2020
Perfect, the last one is what i was looking for.
If I want to make all the matrices visible in one graph. with different colour. How can i do that?

Sign in to comment.

More Answers (1)

Jakob B. Nielsen
Jakob B. Nielsen on 18 Feb 2020
Edited: Jakob B. Nielsen on 18 Feb 2020
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.

  0 Comments

Sign in to comment.

Sign in to answer this question.

Products


Release

R2019b