Help with table variables calculation

2 次查看(过去 30 天)
Hi,
I have a
table 'T'
with a column variable named 'rank', which includes categorical numbers: from 1 to 3.
I want to check if the 'rank' numbers are below or equal to 2. I do that with this:
i12 = (str2num(char(T.rank)) <= 2);
I need help with the following:
If i12 is True, then I want to reassign the rank to be '2'.
So, in the end, I have the variable 'rank' going from 2 to 3.
  1 个评论
Rik
Rik 2022-5-10
编辑:Rik 2022-5-10
I recovered the removed content from the Google cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.
This page is now on archived on the Wayback Machine.

请先登录,再进行评论。

采纳的回答

Clay Swackhamer
Clay Swackhamer 2022-5-9
% Define the table
T = table;
T.rank = [1,3,2,4,2,5,1,3,2,4]';
T.rank = categorical(T.rank);
% Create another column to store the 2s
T.updatedRank = 2*ones(height(T),1);
T.updatedRank = categorical(T.updatedRank);
% Find values <= 2
ix = str2num(char(T.rank)) <= 2; % True for rank 1 and 2
% Replace values
T.rank(ix) = T.updatedRank(ix)
% Get rid of updated rank column (optional)
% T.updatedRank = [];

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by