"A matrix 'B' of 5x5 whose elements are all 5"

My current 'correct' solution to this problem is:
B = [5,5,5,5,5;5,5,5,5,5;5,5,5,5,5;5,5,5,5,5;5,5,5,5,5;]
But this seems a little long and silly. Is there an identity matrix like "M = ones[2,3]" that works for this application?

1 Comment

https://www.mathworks.com/matlabcentral/answers/87577-how-do-i-create-a-10-10-matrix-containing-numbers-from-1-to-100#comment_2335570 I gave 5 answers

Sign in to comment.

Answers (2)

Can you create a matrix of the desired size that is all ones? (Clearly yes, since you know how to use the ones function.) Could you then multiply all elements of that matrix by the number 5? (I hope so.)
As an alternative, could you use the repmat function? (Of course.)
B = 5*ones(5)
B = 5×5
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5

Categories

Products

Release

R2022a

Tags

Asked:

on 31 Aug 2022

Commented:

on 31 Aug 2022

Community Treasure Hunt

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

Start Hunting!