How to create multidimensional array with specified row values

2 次查看(过去 30 天)
Hello, I am stack a little, i would want to an n by n matrix with specified row values, for exemple for a six by 7 matrix, i would like row number one be consisting of only 1s, row ttwo 2s, zand so forth. So that the matrix would look like matrix A inserted below (Note that tis is just an example I want to make a large matrix so that i can use it to manipulate another matrix, please help). Is there a way I can do this for a large matrix without keying in values manually.
A =
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
4 4 4 4 4 4 4
5 5 5 5 5 5 5
6 6 6 6 6 6 6

采纳的回答

Stephen23
Stephen23 2023-5-31
编辑:Stephen23 2023-5-31
m = 6;
n = 7;
A = cumsum(ones(m,n),1)
A = 6×7
1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 6 6 6 6 6 6 6
A = repmat(1:m,n,1).'
A = 6×7
1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 6 6 6 6 6 6 6
A = (1:m).' * ones(1,n)
A = 6×7
1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 6 6 6 6 6 6 6
A = min(1:n,(1:m).')
A = 6×7
1 1 1 1 1 1 1 1 2 2 2 2 2 2 1 2 3 3 3 3 3 1 2 3 4 4 4 4 1 2 3 4 5 5 5 1 2 3 4 5 6 6
A = repmat((1:m).',1,n)
A = 6×7
1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 6 6 6 6 6 6 6
A = repelem((1:m).',1,n)
A = 6×7
1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 6 6 6 6 6 6 6

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by