printmat function for unknown number of rows

1 visualizzazione (ultimi 30 giorni)
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.

Risposta accettata

Cedric
Cedric il 15 Apr 2013
Modificato: Cedric il 15 Apr 2013
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 Commenti
googo
googo il 15 Apr 2013
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 il 15 Apr 2013
Modificato: Cedric il 15 Apr 2013
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 ;-)

Accedi per commentare.

Più risposte (0)

Tag

Non è stata ancora inserito alcun tag.

Community Treasure Hunt

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

Start Hunting!

Translated by