Table: Add only selected collumns

3 vues (au cours des 30 derniers jours)
Nycholas Maia
Nycholas Maia le 8 Jan 2019
Commenté : Nycholas Maia le 8 Jan 2019
% Set a dummy array content:
name = {'Joe'; 'Elisa'; 'John'};
age = [30; 10; 25];
weigth = [100; 150; 180];
% Set 'select_collumns' array:
% The index (1) reference to 'name' array
% The index (2) reference to 'age' array
% The index (3) reference to 'weigth' array
selected = [1, 0, 1];
% Create a table with only select collumns: (1) Name and (3) Weigth:
How could I do it?
% Exemple:
table(name, weigth);
The main ideia is pass a 'select array' that will produce a table with only the select collumns.
How can I do it?

Réponse acceptée

Guillaume
Guillaume le 8 Jan 2019
1. Create the full table first, however you were going to create it:
name = {'Joe'; 'Elisa'; 'John'};
age = [30; 10; 25];
weigth = [100; 150; 180]; %note that weigth is mispelled, should be weight
t = table(name, age, weight)
2. use logical indexing to select the columns you want:
selected = [1, 0, 1];
selected = logical(selected);
%or create it directly as logical:
%selected = [true, false, true];
filteredtable = t(:, selected) %only keep selected columns
  1 commentaire
Nycholas Maia
Nycholas Maia le 8 Jan 2019
Thank you so much Guillaume for your quick response!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by