Help with table variables calculation

2 views (last 30 days)
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 Comment
Rik
Rik on 10 May 2022
Edited: Rik on 10 May 2022
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.

Sign in to comment.

Accepted Answer

Clay Swackhamer
Clay Swackhamer on 9 May 2022
% 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 = [];

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!