How do I generate a given Matrix in one command?

I have to generate a matrix A = [1 1 0 0 0; 1 1 1 0 0; 0 1 1 1 0; 0 0 1 1 1; 0 0 0 1 1]
writing a single command.
Teacher told as some helpful commands would be
EYE(m,n)
ONES(m,n)
ZEROS(m,n)
RAND(m,n)
I give you a beer

댓글 수: 4

Jan
Jan 2011년 2월 23일
As you can see in the web interface at several positions: The name of Matlab is "Matlab".
Do you think, that the answer help you? Or will they just amuse your teacher?
Jan
Jan 2011년 2월 23일
For clarification: Does "I give you a beer" belong to the list of helpful commands or is this a different topic?
+1 vote to marciuc just because of the beer :)
Jan
Jan 2011년 2월 25일
+1: If you can explain how all solutions given here work, you will pass the complete Matlab course based on just one question. Every single answer is funny, but all answers together are serious.

댓글을 달려면 로그인하십시오.

답변 (17개)

Jan
Jan 2011년 2월 23일

7 개 추천

Refering to your former post, which has been deleted now: I know also, who you are: you are marciuc.
At first I suggest this:
A = [1 1 0 0 0;1 1 1 0 0;0 1 1 1 0;0 0 1 1 1;0 0 0 1 1]
This is a single command and it is the most efficient solution: No temporary memory, no overhead for calling commnad, and easy to debug. There is no better solution.
Sean de Wolski
Sean de Wolski 2011년 2월 23일

7 개 추천

Probably the most compact:
A = toeplitz([1 1 0 0 0])

댓글 수: 1

Jan
Jan 2011년 2월 23일
This is the most compact command, except for the ambitious RAND apporach. I vote it.

댓글을 달려면 로그인하십시오.

Jan
Jan 2011년 2월 23일

4 개 추천

Give the beer to your teacher. He is obviously funny if he suggests RAND - but it really works:
A = round(rand(5))
There is at least a certain chance to get the correct answer.

댓글 수: 5

Matt Fig
Matt Fig 2011년 2월 23일
Always a sense of humor.
A = randint(5) would be a shorter way of maybe getting the right answer!
Jan
Jan 2011년 2월 23일
You need the Communications Toolbox for RANDINT.
A small price to pay for elegance!
Jiro Doke
Jiro Doke 2011년 2월 23일
Or if you have MATLAB R2008b or newer,
A = randi([0 1], 5)

댓글을 달려면 로그인하십시오.

Jan
Jan 2011년 2월 23일

4 개 추천

A = dec2bin([24 28 14 7 3]) - '0'

댓글 수: 4

Jiro Doke
Jiro Doke 2011년 2월 23일
+1 vote!
That is sick, wrong, disturbing, and I love it
dec2bin('vzlea'-'^')-'0'
Jan
Jan 2011년 2월 24일
There is the vuvuzela again. The question is obviously more diabolic than I've expected. Again you see: Subtracting zero can reveal the clandestine information.

댓글을 달려면 로그인하십시오.

Matt Tearle
Matt Tearle 2011년 2월 23일

3 개 추천

A = full(gallery('tridiag',ones(1,4),ones(1,5),ones(1,4)))
But my current favorite:
A = 1-reshape(mod(floor((1:25)/3),2),5,5)
Kenneth Eaton
Kenneth Eaton 2011년 2월 28일

3 개 추천

I can't believe no one suggested dilation:
A = imdilate(eye(5),ones(2));
Or convolution:
A = sign(conv2(eye(5),ones(2),'same'));
A = sign(filter2(ones(2),eye(5)));

댓글 수: 1

Jan
Jan 2011년 2월 28일
*You* have suggested it now. So I can't believe it now, too. +1

댓글을 달려면 로그인하십시오.

Jan
Jan 2011년 2월 23일

2 개 추천

A general method to create a diagonal matrix is using DIAG (as the example in "help diag" explains):
A = diag(ones(1, 5)) + diag(ones(1,4), 1) + diag(ones(1,4), -1);
You can discuss, if this is still "a single command".
I do not drink beer. But you can ask your teacher to send me the points gained by solving this homework.

댓글 수: 2

Can I have your beer ?!
You can have all the beer that marciuc's teacher sends me. How's that for a deal?

댓글을 달려면 로그인하십시오.

Paulo Silva
Paulo Silva 2011년 2월 23일

2 개 추천

Here's probably the most awesome way to generate the matrix :D
disp('I dare you to try the Infinite monkey matrix')
answer=input('Press y and Enter if your dare to try','s')
if (strcmp(answer,'y'))
disp('Congratulations your are not a coward')
disp('Good luck')
pause(1)
disp('Please wait or press CTRL+C to cancel')
disp('but canceling the operation makes you a coward!!')
a=[1 1 0 0 0; 1 1 1 0 0; 0 1 1 1 0; 0 0 1 1 1; 0 0 0 1 1];
w=0;b=zeros(5,5);
while ~isequal(a,b)
b=randi([0 1],5,5);
w=w+1;
end
disp('Congratulations we found the Infinite monkey matrix for you')
b
disp('after')
w
disp('attempts')
else
disp('You are a coward!!!!')
end

댓글 수: 1

Yay! The infinite monkey makes its second appearance in a week.

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2011년 2월 25일

2 개 추천

EDIT: line-broken per request.
eval(char(mod(1.0599.^ ...
'i<o<ZC<C<d<d<d_C<C<C<d<d_d<C<C<C<d_d<d<C<C<C_d<d<d<C<Cv', ...
96)))

댓글 수: 1

Jan
Jan 2011년 2월 25일
Thanks for line breaking. This solution will drive marciuc's teacher crazy. Such ugly! +1

댓글을 달려면 로그인하십시오.

Jan
Jan 2011년 2월 23일

1 개 추천

A = abs(bsxfun(@minus, 1:5, transpose(1:5))) < 2
Jan
Jan 2011년 2월 23일

1 개 추천

A = 1 - (tril(ones(5), -2) + triu(ones(5), 2))
Jan
Jan 2011년 2월 24일

1 개 추천

I cannot resist to post some variation of the DEC2BIN theme:
A = dec2bin('8<.''#' - 32) - '0'
A = dec2bin('FJ<51' - 46) - '0'
But finally you can even omit the first subtraction, because DEC2BIN operates on CHAR vectors also, but you cannot type the non-printables directly:
q = [100 101 99 50 98 105 110 40 39 24 28 14 7 3 39 41 45 39 48 39];
clipboard('copy', char(q))
==> Ctrl-v in the command window
>> dec2bin('#####')-'0'
Here the '#' are the non-printables with the ASCII codes [24,28,14,7,3]. You can write them even in a M-file.
Paulo Silva
Paulo Silva 2011년 2월 24일

1 개 추천

b=[0 0 1 1 1
0 0 0 1 1
1 0 0 0 1
1 1 0 0 0
1 1 1 0 0]
A=~b;
Matt Fig
Matt Fig 2011년 2월 24일

1 개 추천

One line, anyway. And since the array is at least dynamically pre-allocated, the code is fast.
for ii = 5:-1:1,for jj = min(ii+1,5):-1:max(ii-1,1),A(ii,jj) = 1;end,end

댓글 수: 2

Jan
Jan 2011년 2월 25일
Brr.
Matt Fig
Matt Fig 2011년 2월 25일
LOL Jan. I am surprised that such an answer is faster than both the BSXFUN and EYE + DIAG + DIAG solutions, even for N=1000.

댓글을 달려면 로그인하십시오.

DGM
DGM 2023년 2월 23일
편집: DGM 2023년 2월 23일
I can't believe that everyone has missed the obvious solution!
rng(46783490); randi([0 1],5,5)
ans = 5×5
1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1
so simple!

댓글 수: 2

+1 very nice. What magic did you use to reverse engineer that?
DGM
DGM 2023년 2월 23일
I made use of the classic "commit to brute force and then go to bed" algorithm.

댓글을 달려면 로그인하십시오.

Paulo Silva
Paulo Silva 2011년 2월 23일

0 개 추천

diag(diag(eye(4,4)),1)+diag(diag(eye(4,4)),-1)+eye(5,5)
or
diag(ones(1,4),1)+diag(ones(1,4),-1)+eye(5,5)
It's similar to the Jan solution above
marciuc
marciuc 2011년 2월 24일

0 개 추천

Thank you guys...regards from Romania

댓글 수: 1

Jan
Jan 2011년 2월 24일
Please do us the favor and accept one of the answers. Or let your teacher choose one.
Beside the fun, this thread will be really helpful, because it describes the creation of tridiagonal matrices exhaustively.
I really hope you had some fun also.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

질문:

2011년 2월 23일

댓글:

DGM
2023년 2월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by