How to create an array

How to create an array
A = [0 0 0 0; 1 1 1 1; 2 2 2 2; 3 3 3 3; 4 4 4 4]
using the command "for" or other?

 Accepted Answer

Use the repmat function:
A = repmat(0:4, 4, 1)';
A =
0 0 0 0
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4

2 Comments

JRC
JRC on 19 Jul 2017
Perfect Star Strider!
I didn´t know the command "repmat".
Thank you.
As always, my pleasure!
Another option for your particular problem is to use element-wise vector multiplication:
A = [0:4]'.*ones(1,4);
See the documentation on Array vs. Matrix Operations (link) for details.

Sign in to comment.

More Answers (0)

Categories

Find more on Operators and Elementary Operations in Help Center and File Exchange

Tags

Asked:

JRC
on 19 Jul 2017

Commented:

on 19 Jul 2017

Community Treasure Hunt

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

Start Hunting!