Help with table variables calculation

2 visualizzazioni (ultimi 30 giorni)
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 Commento
Rik
Rik il 10 Mag 2022
Modificato: Rik il 10 Mag 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.

Accedi per commentare.

Risposta accettata

Clay Swackhamer
Clay Swackhamer il 9 Mag 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 = [];

Più risposte (0)

Categorie

Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange

Prodotti


Release

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by