printmat function for unknown number of rows

1 次查看(过去 30 天)
Example:
M= rand(5)
printmat(M, 'My Matrix', 'ROW1 ROW2 ROW3 ROW4 ROW5', 'FOO BAR BAZ BUZZ FUZZ' )
but suppose I don't know how many rows I will have (it depends on the user input) what can I do in this situation?
Thank's.

采纳的回答

Cedric
Cedric 2013-4-15
编辑:Cedric 2013-4-15
You can build your headers using SPRINTF; for example:
>> M = rand(6) ;
>> vheader = sprintf('ROW%d ', 1:size(M,1))
vheader =
ROW1 ROW2 ROW3 ROW4 ROW5 ROW6
(you might want to STRTRIM the output to get rid of the last white space)
  2 个评论
googo
googo 2013-4-15
Hey, thank's but:
where should I write it?
here is my code :
function hw = homework(a,b)
U =unifrnd(a,b); % Generates a number from the uniform distribution
n =input('Enter a requested sample size:');
r = input('Enter a requested number of repeats:');
stats = zeros(r,2);
M = exprnd(1/U,r,n); % Generates a r(x)n matrix of exponential
for i=1:r
stats(i,1) = 1/mean(M(i,1:n)); % caculates the MLE for a row sample
stats(i,2) = (sqrt(n) * (stats(i,1) - U))/U;
end
display(U);
hw = stats;
end
the matrix is rX2 and I want the titles to be:
#sample MLE T
1
2
3
4
r
can you explain a little bit more? thank's!
Cedric
Cedric 2013-4-15
编辑:Cedric 2013-4-15
Well, in your example, you have the following call:
printmat(M, 'My Matrix', 'ROW1 ROW2 ROW3 ROW4 ROW5', 'FOO BAR BAZ BUZZ FUZZ')
where M is a numeric array, and the next 3 arguments are strings. I assume that 'ROW1 ROW2 ROW3 ROW4 ROW5' is the string defining the vertical header in the output, and you could actually have it saved in a variable that you pass to this function printmat of yours:
vheader = 'ROW1 ROW2 ROW3 ROW4 ROW5' ;
printmat(M, 'My Matrix', vheader, 'FOO BAR BAZ BUZZ FUZZ')
and more generally:
label = 'My Matrix'
vheader = 'ROW1 ROW2 ROW3 ROW4 ROW5' ;
hheader = 'FOO BAR BAZ BUZZ FUZZ'
printmat(M, label, vheader, hheader) ;
Now the example that I gave shows you how to build vheader if you don't know the number of rows of M in advance:
label = 'My Matrix'
vheader = sprintf('ROW%d ', 1:size(M,1)) ;
hheader = 'FOO BAR BAZ BUZZ FUZZ'
printmat(M, label, vheader, hheader) ;
Now it's up to you to adapt this to your homework ;-)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by