How separate data from a table and convert every column in vector if this seáration depends on the label 'D' or 'E'?

조회 수: 7 (최근 30일)
How I separate a table if I need the corresponding values from 'D' and 'E' in diferents vectors
angle1 angle2 angle3 label
--------- ----------- -------- ----------- --------
111 90 50 'D' ...
321 80 50 'E'
121 70 50 'E'
222 70 50 'D'
333 80 50 'D'
I need obtain the vector for every colum depends of the label 'D' and 'E' for instance
angle1E = [321 121]
angle1D = [111 222 333]
angle2E = [80 70]
angle2D = [90 70 80]
I tried first change the colum 4 in a categorical, and after this use a called in the colum all D but i dont understand well
close all;
clearvars;
clc;
%% import data for angle
angle = readtable('angulos33.txt');
%% Variable
A11 = angle(:,1); %%angle at 0 h
A22 = angle(:,2); %%angle at 24 h
A33 = angle(:,3); %% Angle at 48 h
A44 = angle(:,4); %%etiqueta interno "D" externo "E"
A44 = categorical(angle.Var4('D'))

채택된 답변

Star Strider
Star Strider 2020년 6월 24일
Try this:
C = { 111 90 50 'D'
321 80 50 'E'
121 70 50 'E'
222 70 50 'D'
333 80 50 'D'}; % Copy-Paste Data
T = cell2table(C, 'VariableNames',{'angle1','angle2','angle3','label'}); % Create Table
G = findgroups(T{:,4}); % Could Also Use ‘unique’
Angles = accumarray(G, (1:numel(G)).',[], @(x){T(x,:)}); % Group Data Into Separate Tables
then defining:
AnglesD = Angles{1}
AnglesE = Angles{2}
produces:
AnglesD =
3×4 table
angle1 angle2 angle3 label
______ ______ ______ _____
111 90 50 {'D'}
222 70 50 {'D'}
333 80 50 {'D'}
AnglesE =
2×4 table
angle1 angle2 angle3 label
______ ______ ______ _____
321 80 50 {'E'}
121 70 50 {'E'}
To extract them from the tables:
Angle1D = AnglesD.angle1.'
Angle1E = AnglesE.angle1.'
producing:
Angle1D =
111 222 333
Angle1E =
321 121
and similarly for the others.
.

추가 답변 (1개)

Takumi
Takumi 2020년 6월 24일
Using strcmp function, you can get the index that matches the specified string.
You can then use the index to find the corresponding value.
angle1 = [111;321;121;222;333];
angle2 = [90;80;70;70;80];
angle3 = [50;50;50;50;50];
label = {'D';'E';'E';'D';'D'};
yourtable = table(angle1,angle2,angle3,label);
yourtable.angle1(strcmp(yourtable.label,'D'))

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by