printmat function for unknown number of rows

조회 수: 1 (최근 30일)
googo
googo 2013년 4월 15일
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