Using ranges in Matlab / Octave matrices

Let's say I want to create a 100x100 matrix, from which each row contains elements 1-100

A = [1:100; 1:100; 1:100... n]

Obviously, forming a matrix is ​​a bad idea because it will force me to create 100 rows in a 1: 100 range.

I think I could do this by taking a "single" array and multiplying each row by vector ... but I'm not sure how to do this

a = (ones(100,100))*([])

??

Any tips?

+3
source share
3 answers

You can use the repeat matrix function ( repmat()). Then the code will look like this:

A = repmat( 1:100, 100, 1 );

This means that you repeat the first argument repmat100 times vertically and once horizontally (i.e. you leave it as horizontal).

+5
source

- 100 1 1:100.

ones(3,1)*(1:3)
ans =

   1   2   3 
   1   2   3
   1   2   3

repmat ([edit], Phonon [/edit]).

+4

Yes, repmat is a simple solution and maybe even the right solution. But knowing how to visualize your goal and how to create something that gives this goal will provide long-term benefits in MATLAB. So try other solutions. For instance...

cumsum(ones(100),2)

bsxfun(@plus,zeros(100,1),1:100)

ones(100,1)*(1:100)

cell2mat(repmat({1:100},100,1))

and boring

repmat(1:100,100,1)
+2
source

All Articles