Info

此问题已关闭。 请重新打开它进行编辑或回答。

hello everyone, i have a problem in labeling the matrix.

1 次查看(过去 30 天)
let's suppose i have 4x6 matrix, i want to assign each row as [1 2 3 4] similarly column [1 2 3 4 5 6] with some extra space with current matrix so that its not be seen as the part of matrix and at the end,matrix dimension should not change(i.e. 4x6). how can i do? can u help please? for example
d= 11 12 13 14 15 16
17 18 19 21 22 24
27 28 25 2 44 45
23 23 23 3 3 3
d is a matrix, i want to represent this as
d=
1 2 3 4 5 6
1 11 12 13 14 15 16
2 17 18 19 21 22 24
3 27 28 25 2 44 45
4 23 23 3 3 3 3
but the important thing is, i don't want 1:6 Z& 1:4 as a part of matrix,i.e. matrix dimension should not change i.e. 4x6,only additional part is, matrix d has labeled now.
  5 个评论
AMIT BHASKAR
AMIT BHASKAR 2015-10-30
for ease of understanding i change the value of matrix from 20x40 to 4X6 with example. thanks
Ilham Hardy
Ilham Hardy 2015-10-30
Firstly, that's not how the flag works.
Secondly, WHY do you want to do that?

回答(2 个)

Ced
Ced 2015-10-30
This is ugly, but should do the trick. Not sure why you want this though...
function print_my_mat(A)
nrows = size(A,1);
ncols = size(A,2);
my_cell = cell(nrows+1,ncols+1);
my_cell(1,2:end) = num2cell(1:ncols); % number your columns
my_cell(2:end,1) = num2cell(1:nrows); % number your rows
my_cell(2:end,2:end) = num2cell(A); % write your matrix inside cell
for i = 1:nrows
% format your numbers as desired and save as string
temp = cellfun(@(x)num2str(x,'%i'),my_cell(i,:),'UniformOutput',0);
% print row
cellfun(@(x)fprintf('%4s',x),temp,'UniformOutput',0);
% next line
fprintf('\n')
end
end

Walter Roberson
Walter Roberson 2015-11-7
You would need to define a new data class of labeled matrix. You could possibly subclass from double precision so that you would not have to re-code all of the numeric operations. You would have to code a new display() method for the class.
But it is easier by far to just write a small bit of code that displays the matrix in that form when you ask it to.

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by