How to create a square matrix with consecutive numbers on each row?

Hi everyone,
Given a vector i.e. n=[1 12 25 78], is there any way to create a matrix A, such that
A=[ 1 2 3 4; 11 12 13 14; 23 24 25 26; 75 76 77 78]?
without FOR LOOP?

 採用された回答

Guillaume
Guillaume 2015 年 8 月 20 日
編集済み: Guillaume 2015 年 8 月 20 日

0 投票

With toeplitz construct a symmetric matrix with 0 on diagonal and increments on the sides and with bsxfun add that to your n:
n = [1 12 25 78];
A = bsxfun(@plus, toeplitz(0:-1:1-numel(n), 0:numel(n)-1), n')

その他の回答 (1 件)

Sebastian Castro
Sebastian Castro 2015 年 8 月 20 日

0 投票

Yeah, for sure.
I'm sure there are more efficient ways to do this, but this one will show you a few examples of the "repmat" function to string together vectors and matrices (either row-wise or column-wise).
I first avoided hard-coding parameters by using a variable "nCols" for number of columns, which should be the same as number of rows (or numel(n)). Note that I had to transpose n to n' to meet your desired solution.
>> nCols = numel(n);
>> baseMatrix = repmat(n',[1 nCols])
baseMatrix =
1 1 1 1
12 12 12 12
25 25 25 25
78 78 78 78
Next, you have to make the pretty complicated matrix to add to that matrix above. I would copy-paste both of the terms below into MATLAB to see what each of those does. Basically, I create a column pattern and then a row pattern, and subtract them.
>> addMatrix = repmat(0:nCols-1,[nCols 1]) - repmat((0:nCols-1)',[1 nCols])
addMatrix =
0 1 2 3
-1 0 1 2
-2 -1 0 1
-3 -2 -1 0
Finally, add 'em up!
>> A = baseMatrix + addMatrix
A =
1 2 3 4
11 12 13 14
23 24 25 26
75 76 77 78
- Sebastian

4 件のコメント

Ming
Ming 2015 年 8 月 21 日
Thanks a lot!
David Alejandro Ramirez Cajigas
David Alejandro Ramirez Cajigas 2021 年 8 月 18 日
a matrix of size NxN, whose diagonal is zero and from each "0" there is a consecutive list of numbers up to N
for example the following, after every 0 there is a consecutive numbering up to N
N = 22
but suppose that N is any number
How do I make it in matlab automatically giving only the number N?
N = 22;
v = [0:N];
M = toeplitz([v(1) fliplr(v(2:end))], v)
M = 23×23
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 21 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 20 21 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 19 20 21 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 18 19 20 21 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 17 18 19 20 21 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 16 17 18 19 20 21 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 15 16 17 18 19 20 21 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 14 15 16 17 18 19 20 21 22 0 1 2 3 4 5 6 7 8 9 10 11 12 13
result = mod(tril(-tril(M)) + triu(M), N+1)
result = 23×23
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 3 2 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 9 8 7 6 5 4 3 2 1 0 1 2 3 4 5 6 7 8 9 10 11 12 13
David Alejandro Ramirez Cajigas
David Alejandro Ramirez Cajigas 2021 年 8 月 18 日
編集済み: David Alejandro Ramirez Cajigas 2021 年 8 月 18 日
Bingo!
The answer is:
N=22
Top1=N
Top12=repmat(0:Top1-1,[Top1 1]) - repmat((0:Top1-1)',[1 Top1]); %genera matriz 0 hasta n
Top17=(tril(Top12,-1)*-1);
Top18=Top17+Top12;
Top19=Top17+Top18

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by