How to extract the name of the column and row from a table?
6 views (last 30 days)
Show older comments
I generated a code that extracts all the 2x2 submatrices from a 10x7 matrix while keeping the order. The original matrix is conveted into a table where each row and each column has a name. I want each column and row in each of the 2x2 submatrices to have its corresponding name as the original matrix 'sTable' (the same name as the original matrix). Here's my code:
Any help is appreciated!
clear
clc
m=rand(10,7)
rowNames = {'C1','C2','C3','C4','C5','C6','C7','C8','C9','C10'};
colNames = {'N1','N2','N3','N4','N5','N6','N7'};
sTable = array2table(m,'RowNames',rowNames,'VariableNames',colNames);
[r, c]= size(m);
sub= {};
for s=1:r-1
for n=s+1:r-s
for j=1:c
for i=j+1:c
sub{end+1}=m([s n],[j i]);
end
end
end
end
0 Comments
Accepted Answer
Walter Roberson
on 27 Feb 2023
clear
clc
m=rand(10,7)
rowNames = {'C1','C2','C3','C4','C5','C6','C7','C8','C9','C10'};
colNames = {'N1','N2','N3','N4','N5','N6','N7'};
sTable = array2table(m,'RowNames',rowNames,'VariableNames',colNames);
[r, c]= size(m);
sub= {};
for s=1:r-1
for n=s+1:r-s
for j=1:c
for i=j+1:c
sub{end+1} = array2table(m([s n],[j i]), 'VariableNames', colNames([j i]), 'RowNames', rowNames([s n]));
end
end
end
end
sub{1}
sub{2}
sub{floor(end/2)}
More Answers (0)
See Also
Categories
Find more on Tables 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!