How to separate four columns on the basis of 5th column

조회 수: 3 (최근 30일)
Med Future
Med Future 2023년 6월 1일
편집: Stephen23 2023년 6월 1일
Hello, I hope you are doing well. I have the following Dataset in which I have five column. I want to seperate the data based on fifth column.
Like if the fifth column has the value 1, then all the four column related to value 1 save in new cell.

답변 (2개)

Harsh Saxena
Harsh Saxena 2023년 6월 1일
Hi,
To do this you can use the following logic:
% just to load data
data = load('Dataset.mat');
x = data.Dataset;
%
A = x([x(:, 5)] == 1, :);
A will be an array which will contain only the rows with fifth column element as 1.
Hope this helps!
  댓글 수: 1
Med Future
Med Future 2023년 6월 1일
@Harsh Saxena I want this for all values in 5th column. Can you please modified the code and save each in single cell array

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


Stephen23
Stephen23 2023년 6월 1일
편집: Stephen23 2023년 6월 1일
format short G
S = load('Dataset.mat');
D = S.Dataset
D = 261132×5
1.0e+00 * 997.36 819.74 0.065929 1.205 4 1013.2 889.99 0.0075226 3.995 1 1097.4 815.81 0.064062 1.195 4 1197.4 819.75 0.06587 1.205 4 1297.4 819.74 0.066081 1.205 4 1397.4 817.39 0.067609 1.195 4 1497.4 818.96 0.068112 1.205 4 1513.2 889.99 0.0075284 8 1 1597.4 815.8 0.063999 1.195 4 1697.4 817.01 0.066855 1.195 4
Method one: ARRAYFUN:
V = D(:,5);
U = unique(V)
U = 9×1
1 3 4 6 7 10 11 15 16
C = arrayfun(@(x)D(x==V,:),U,'uni',0)
C = 9×1 cell array
{17609×5 double} { 1033×5 double} {62557×5 double} { 6123×5 double} {53445×5 double} {70500×5 double} {27394×5 double} { 6798×5 double} {15673×5 double}
Method two: ACCUMARRAY:
V = D(:,5);
W = (1:numel(V));
C = accumarray(V,W(:),[],@(x){D(x,:)})
C = 16×1 cell array
{17609×5 double} { 0×0 double} { 1033×5 double} {62557×5 double} { 0×0 double} { 6123×5 double} {53445×5 double} { 0×0 double} { 0×0 double} {70500×5 double} {27394×5 double} { 0×0 double} { 0×0 double} { 0×0 double} { 6798×5 double} {15673×5 double}

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by