I'm trying to understand your tables command bugs
1 次查看(过去 30 天)
显示 更早的评论
% Why does this happen?
A = randi([0 20],5, 5)
table(A,'RowNames',{'a' 'b' 'c' 'd' 'e'}) % doesn't work
table(A(:,1)','RowNames',{'a' 'b' 'c' 'd' 'e'},'VariableNames',{'a'}) % Doesn't work
table(A(1,:)','RowNames',{'a' 'b' 'c' 'd' 'e'},'VariableNames',{'a'}) % Works
% Who's writing this software?
0 个评论
采纳的回答
Steven Lord
2020-2-20
What specifically does "doesn't work" mean? If you receive errors when you try running those commands, please show the full and exact text of the error messages you receive.
The first one works when I run it in release R2019b and creates the table array I expected, with five rows and one variable. If you expected it to have five rows and five variables, use array2table instead.
array2table(A, 'RowNames', {'a','b','c','d','e'}, 'VariableNames', {'f', 'g', 'h', 'i', 'j'})
The second one correctly throws an error. A(:, 1) is a 5-by-1 vector so A(:, 1)' is a 1-by-5 vector. A table with one row can't have five RowNames. If you specified just one RowNames entry, it would work and create a table with one row and one variable.
table(A(:,1)','RowNames',{'a'},'VariableNames',{'b'})
If you expected it to create a table with one row and five variables, again use array2table instead.
array2table(A(:, 1)', 'RowNames', {'row1'}, 'VariableNames', {'a','b','c','d','e'})
The third one also works for me, creating a table with five rows and one variable.
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!