# divide a matrix based on the presence of a coefficient

2 views (last 30 days)
Giuseppe D'Amico on 23 Apr 2021
Commented: Chunru on 26 Apr 2021
Hi everyone, I need a hand.
I have 87600 x 4 matrices where, in the fourth column, there is a value that differentiates between holidays = 0.8, pre-holidays = 0.9 and working days = 1.
How is it possible to create a script that allows you to divide the starting matrix into three matrices?
Where a matrix contains all the rows to which the fourth column is associated with the index 0.8, the second matrix contains all the rows in which the fourth column is associated with the index 0.9 and the third all the rows in which the last column is associated with a 1.
I hope I was clear in the question, an infinite thanks to those who know how to help me.
##### 2 CommentsShow NoneHide None
Giuseppe D'Amico on 23 Apr 2021
heeelp
Giuseppe D'Amico on 23 Apr 2021
load input
festivi = cell([],4);
[m,n] = size(input);
for i = 1:m
if input(:,4)== 0.8
count = count+1;
festivi{count,1} = input;
end
end
I've tried this, but don't work. What should i change?

Sign in to comment.

### Answers (2)

Chunru on 23 Apr 2021
A = randi(5, [80, 4]); % your data
u = unique(A(:, 4)); % unique values of column 4
for i=1:length(u)
B{i,1} = A( A(:,4) == u(i), :);
end
B
##### 2 CommentsShow NoneHide None
Giuseppe D'Amico on 23 Apr 2021
sorry I don't understand how to use it
Chunru on 26 Apr 2021
(1) Change the first line to your own data. I am using some random generated data for testing.
(2) Line 2 find the unique values of column 4
(3) The rest of code groups the matrix according to value in column 4 and assign the reults into a cell array.

Sign in to comment.

Steven Lord on 23 Apr 2021
Do you need these as separate variables or do you just need to process each group of dates separately? In the latter case, see the findgroups, splitapply, groupsummary, and grouptransform functions and the functions listed in the "See Also" sections on their documentation pages.
Creating three variables from an array is probably okay, but creating a dynamic number of variables (when or if you start separating US holidays from UK holidays from state holidays from ...) is straying into discouraged territory.
##### 1 CommentShow -1 older commentsHide -1 older comments
Giuseppe D'Amico on 23 Apr 2021
I have for example a matrix created in the following way.
What I want is to divide it according to the value present in the fourth column, creating 3 different matrices.
input= [-600 1 0 0.8
-600 1 0 0.8
-600 1 0 0.8
-588 1 0 0.8
-588 1 1 0.9
-564 1 1 0.9
-540 1 1 0.9
-552 1 1 1
-516 1 2 1
-504 1 2 1
-480 1 2 1]
So:
A =[-600 1 0 0.8
-600 1 0 0.8
-600 1 0 0.8
-588 1 0 0.8]
B = [-588 1 1 0.9
-564 1 1 0.9
-540 1 1 0.9]
C = [-552 1 1 1
-516 1 2 1
-504 1 2 1
-480 1 2 1]

Sign in to comment.

### Categories

Find more on Data Preprocessing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!