Help with table variables calculation

2 visualizaciones (últimos 30 días)
Nina Perf
Nina Perf el 9 de Mayo de 2022
Editada: Nina Perf el 14 de Mayo de 2022
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 comentario
Rik
Rik el 10 de Mayo de 2022
Editada: Rik el 10 de Mayo de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Clay Swackhamer
Clay Swackhamer el 9 de Mayo de 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 = [];

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by