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

3 次查看(过去 30 天)
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