32 views (last 30 days)

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

Jan
on 23 Feb 2011

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.

Matt Tearle
on 23 Feb 2011

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
on 28 Feb 2011

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)));

Jan
on 23 Feb 2011

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.

Matt Tearle
on 23 Feb 2011

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

Sign in to comment.

Paulo Silva
on 23 Feb 2011

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

Jan
on 24 Feb 2011

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.

Sign in to comment.

Matt Fig
on 24 Feb 2011

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

Walter Roberson
on 25 Feb 2011

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)))

Paulo Silva
on 23 Feb 2011

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

Sign in to comment.

Paulo Silva
on 24 Feb 2011

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;

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/1881-how-do-i-generate-a-given-matrix-in-one-command#comment_3526

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/1881-how-do-i-generate-a-given-matrix-in-one-command#comment_3526

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/1881-how-do-i-generate-a-given-matrix-in-one-command#comment_3547

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/1881-how-do-i-generate-a-given-matrix-in-one-command#comment_3547

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/1881-how-do-i-generate-a-given-matrix-in-one-command#comment_3839

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/1881-how-do-i-generate-a-given-matrix-in-one-command#comment_3839

## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/1881-how-do-i-generate-a-given-matrix-in-one-command#comment_3942

⋮## Direct link to this comment

https://it.mathworks.com/matlabcentral/answers/1881-how-do-i-generate-a-given-matrix-in-one-command#comment_3942

Sign in to comment.