# divide a matrix based on the presence of a coefficient

조회 수: 1(최근 30일)
Giuseppe D'Amico 2021년 4월 23일
댓글: Chunru 2021년 4월 26일
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표시숨기기 이전 댓글 수: 1
Giuseppe D'Amico 2021년 4월 23일
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?

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

### 답변(2개)

Chunru 2021년 4월 23일
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표시숨기기 이전 댓글 수: 1
Chunru 2021년 4월 26일
(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.

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

Steven Lord 2021년 4월 23일
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표시숨기기 없음
Giuseppe D'Amico 2021년 4월 23일
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]

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

### Community Treasure Hunt

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

Start Hunting!

Translated by