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
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.
채택된 답변
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})
댓글 수: 3
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)
추가 답변 (1개)
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 Center 및 File Exchange에서 Categorical Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!