Clear Filters
Clear Filters

Hi, I have a 150*54 matrix and a table of 18 labels ,and i want to named each 3 column of my matrix(which is 54) by 1 label

2 views (last 30 days)
Hi, I have a 150*54 matrix and a table of 18 labels ,and i want to named each 3 column of my matrix(which is 54) by 1 label ,how it possible ? Thanks in advance

Accepted Answer

Walter Roberson
Walter Roberson on 20 Sep 2017
Is column 3 a numeric index into the label list?
%prepare some data for test purposes
your_labels = {'a', 'and', 'because', 'excessively', 'hamster', 'manager', 'me', 'not', 'obeyed', 'smart', 'some', 'system', 'tall', 'terrified', 'the', 'to', 'told', 'young'}; %18 labels
your_matrix = rand(150, 54);
your_matrix(:,3) = randi(length(your_labels), 150, 1);
%now do the work
V = @(M) M(:);
result = [num2cell(your_matrix(:,1:2),2), V(your_labels(your_matrix(:,3))), num2cell(your_matrix(:,4:end),2)];
Though you might prefer,
result = [V(your_labels(your_matrix(:,3))), num2cell(your_matrix(:,[1:2,4:end]),2)];

More Answers (1)

KL
KL on 19 Sep 2017
your_matrix = rand(150,54);
C = mat2cell(your_matrix,150,repmat(3,1,18));
% your_labels = 1x18 cellarray
T = cell2table(C,'VariableNames',your_labels);
  4 Comments
KL
KL on 20 Sep 2017
Or just use an array of structs..
your_matrix = rand(150,54);
C = mat2cell(your_matrix,150,repmat(3,1,18));
your_labels = {'a', 'and', 'because', 'excessively', 'hamster', 'manager', 'me', 'not', 'obeyed', 'smart', 'some', 'system', 'tall', 'terrified', 'the', 'to', 'told', 'young'}; %18 labels
for k=1:numel(your_labels)
labelled_data(k).label = your_labels(k);
labelled_data(k).values = C{k};
end

Sign in to comment.

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!