필터 지우기
필터 지우기

accessing table data in different forms

조회 수: 1 (최근 30일)
Ege
Ege 2014년 10월 25일
답변: Ege 2014년 10월 26일
Hi I have T table as follows.
LastName = {'Smith';'Johnson';'Williams';'Jones';'Brown'};
Age = [38;43;38;40;49];
FirstName = {'Amanda' ;'Brenda';'Carl'; 'Denis'; 'Ethan'};
Something = {'String1' ;'String2';'String2'; 'String1'; 'String5'};
Weight = [176;163;131;133;119];
FavoriteColor = {'blue' ;'red' ;'yellow'; 'orange' ;'colorblind' };
T = table(Age,FirstName,Weight,FavoriteColor,Something,'RowNames',LastName)
T.FavoriteColor= categorical(T.FavoriteColor);
T.Something= categorical(T.Something);
when I use
A=T(:,5);
I get a variable A which is a table as well. But when I use
A=T.Something;
I get a variable A which is a categorical value like the ones is column Something. I want to use loops so I need to use the first one with indices but I want the result in the second one. What should I do?
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2014년 10월 25일
Ege - what are you trying to achieve? You mention that you want to use loops, but what do you want to use the loops for?
Ege
Ege 2014년 10월 25일
Well I just started the project I will use it for many things but for now, I have divided my dataset into 2 tables which holds categorical and non categorical data. I'm currently using the categorical one and try to implement a histogram on that since usual histogram function does not work on categorical data. My dataset is huge so at first I will use the categorical column of the T table I mentioned in the question. When I use it like below, it works fine.
a=T.Something;
hist=zeros(size(a,1),1);
u=unique(a);
for i=1: size(a,1)
for j=1: size(u,1)
if isequal(a(i),u(j))
hist(j)=hist(j)+1;
end
end
end
for i=1:numel(hist,1)
if isequal(hist(i),0)
hist(i)=[];
end
end
histogram = table(u(:),hist(:));
But when I try using
a=T(:,5);
the histogram does not work I get wrong results in hist().

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

채택된 답변

Ege
Ege 2014년 10월 26일
accesing it like
a=T{:,5};
solved my problem. Hope this helps someone as well :D

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by