How to split a matrix into n smaller matrices

3 views (last 30 days)
Josh
Josh on 18 Apr 2014
Answered: Image Analyst on 18 Apr 2014
Hello,
Intro:
I am importing data from a text file, so depending on the file the size of my matrix changes (number of columns remains the same), and splitting/searching for the values I am looking for by a specified row/column seem to be out of the question; I have a matrix, example:
A =
[2.1 3.4 5.6 1;
2.1 3.3 5.7 1;
3.3 2.2 4.4 2]
Problem:
I would like to split the matrix into smaller portions based on the value of the last column (i.e. I would like a series of matrices which contain only the values that contain a 1, 2,..., n in their last column)
Current Approach:
The best way I can think to do this is by scanning through each row element of column 4 (in this example) and determining when the integer number changes to then split the parent matrix according to which row the integers change at.
if true
% for b = 1:length(A)
if A(b,4) ~= A(b+1,4)
%assign inequality to some variable
end
Thanks

Answers (1)

Image Analyst
Image Analyst on 18 Apr 2014
Try this:
A = [2.1 3.4 5.6 1;
2.1 3.3 5.7 1;
3.3 2.2 4.4 2]
rowsToKeep = A(:,end)==2 % Find rows where last column = 2.
output = A(rowsToKeep, :) % Extract only those rows.

Community Treasure Hunt

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

Start Hunting!