Shorten the values from a column in another column.

1 次查看(过去 30 天)
Hello,
I have a column with 4 million values. These are categorized in 800 unique values, for example 01.010, 01.020, 05.010, 05.020 etc. I want to make a new column that shortens the unique values. So instead of it containing 01.010 and 01.020 values, I want the new column to say 01 when the values are between 01.000 and 01.999.
And the same for 02.000 .... to 02.999. So this should only assign it the value 02 instead.
I can´t figure out the way to do this and was hoping someone could help me with the coding.
Thank you!
  2 个评论
the cyclist
the cyclist 2021-5-25
Are your inputs numerical values, or text? (I'm asking because MATLAB would not ordinarily display a number as 01.010.)
Can you give a small example of an input and expected output? And perhaps you could upload your full dataset in a MAT file, using the paperclip symbol in from the INSERT section of the toolbar.
Amina Ag
Amina Ag 2021-5-25
The datafile is text but i have exported a short version of it into excel (also added here). Some of the variables are cell and have been transformed to categorical variables. So the specific one I am talking about here is item_50108 which is industry code. And I would like to shorten them from being very specific industries to only containing the main industry code, which would be the 2 first values of the 5.

请先登录,再进行评论。

采纳的回答

the cyclist
the cyclist 2021-5-25
编辑:the cyclist 2021-5-26
The specific solution is potentially very dependent on ...
  • the input format -- there are several way to read in Excel, leading to different variables types in the workspace
  • what exactly you want as an output format -- numeric, character array, string?
It might be better to upload a MAT file with what you already have in your MATLAB workspace, instead of an export of something you have already imported.
However, here is a solution that reads in the Excel, and convert the longer character array to a character with only the numeric characters prior to the decimal point.
% Read the Excel file [This points to the file you uploaded here.]
B = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/629905/B.xlsx');
% Extract the regular expresion
out = regexp(B.item_50108,'\d*(?=\.)','match');
% Display the first value, as a check
disp(out{1})
{'63'}
  3 个评论
the cyclist
the cyclist 2021-5-26
So, the problem is that the output of my command is a cell array of 1x1 cell arrays. (I'm not clear why that is, to be frank.)
I am certain there is a better solution, but here is a quick fix:
% data = readtable('B.txt'); % You should use this instead of the next line, since you have the file locally
data = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/630650/B.txt');
cellOfCells = regexp(data.item_50108,'\d*(?=\.)','match');
cellOfChar = cellfun(@(x)x{:},cellOfCells,'UniformOutput',false);
out = categorical(cellOfChar)
out = 30×1 categorical array
63 51 00 45 51 22 70 22 60 22 70 50 52 51 50 15 63 55 28 52 52 45 51 45 45 51 52 52 70 67

请先登录,再进行评论。

更多回答(1 个)

the cyclist
the cyclist 2021-5-25
If these are numeric values, I think maybe you can do
output_values = unique(floor(input_values));
But see my comment above, for clarifications.

类别

Help CenterFile Exchange 中查找有关 Categorical Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by