Trying to get a list of the different variables in a column

조회 수: 29 (최근 30일)
Alice Brenton
Alice Brenton 2021년 9월 29일
댓글: Cris LaPierre 2021년 9월 30일
I am trying to create an array which displays the different types of variables i have on one of my columns labelled 'Five' in a table i have. the variables are airport names so i just need to see how many different airports i am collecting different data from. The table is added here to show what i'm working with.
The code i cuurently have is this:
clc
clear all
raw_data = readtable('n10013911.csv');
i = 1;
acronym = zeros(10,1);
for loop = acronym
for index = raw_data(:,'Five')
ii=1
if index == acronym(ii)
ii=ii+1;
else
acronym(i,1) = raw_data(ii,'Five');
i = i + 1;
ii=ii+1;
end
end
end
I'm not the best at for loops and if statements, but this one just doesnt seem to want to work

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 9월 29일
편집: Cris LaPierre 2021년 9월 29일
Turn your data into categoricals, then use the categories function to list the unique categories.
Five = {'LHR';'LHR';'PEK';'SYD'};
Five = categorical(Five);
categories(Five)
ans = 3×1 cell array
{'LHR'} {'PEK'} {'SYD'}
Another option may be to use the unique function on the data as you currently have it.
Five = {'LHR';'LHR';'PEK';'SYD'};
unique(Five)
ans = 3×1 cell array
{'LHR'} {'PEK'} {'SYD'}
  댓글 수: 1
Alice Brenton
Alice Brenton 2021년 9월 30일
Thank you, both of these options worked for me, and I just went with the first way.

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

추가 답변 (1개)

Mathieu NOE
Mathieu NOE 2021년 9월 29일
hello
if I understand correctly, you want to extract the list of unique values of the "five" column and store that in an array
you can easily get this by doing :
Five_array = unique(T.Five); % assuming your table is T
  댓글 수: 2
Alice Brenton
Alice Brenton 2021년 9월 30일
This one works as well, i just have to turn it into a categorical first. Thank you!
Cris LaPierre
Cris LaPierre 2021년 9월 30일
Unique will also work on strings and character arrays, meaning you do not have to convert your data into a categorical first if you use this approach.

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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by