Hi. I am a complete novice at matlab and have been asked to create a 10x10 matrix in as few steps as possible. The first row has to be from 1:10, the second line from 2:11 and so on. How can I create this using repmat?

1 回表示 (過去 30 日間)
%this is the matrix I want to achieve
A = zeros(10)
A(1,:)=[1:10]
A(2,:)=[2:11]
A(3,:)=[3:12]
A(4,:)=[4:13]
A(5,:)=[5:14]
A(6,:)=[6:15]
A(7,:)=[7:16]
A(8,:)=[8:17]
A(9,:)=[9:18]
A(10,:)=[10:19]
%How can I achieve this using repmat, reshape or meshgrid and the colon
%operator?
  1 件のコメント
Stephen23
Stephen23 2019 年 2 月 22 日
"How can I achieve this using repmat, reshape or meshgrid and the colon operator?"
Are those really the only operators you are allowed to use?

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

採用された回答

Fangjun Jiang
Fangjun Jiang 2019 年 2 月 21 日
(1:10)+(0:9)'
  2 件のコメント
Sam Thorpe
Sam Thorpe 2019 年 2 月 21 日
Thanks Fanjung, but is there any way of doing this using the functions which I specified?
Fangjun Jiang
Fangjun Jiang 2019 年 2 月 22 日
It implies
repmat(1:10,10,1)+repmat((0:9)',1,10)

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

その他の回答 (3 件)

Stephen23
Stephen23 2019 年 2 月 22 日
This uses meshgrid and plus as well (not sure if that is allowed):
>> [X,Y] = meshgrid(0:9,1:10);
>> X+Y
ans =
1 2 3 4 5 6 7 8 9 10
2 3 4 5 6 7 8 9 10 11
3 4 5 6 7 8 9 10 11 12
4 5 6 7 8 9 10 11 12 13
5 6 7 8 9 10 11 12 13 14
6 7 8 9 10 11 12 13 14 15
7 8 9 10 11 12 13 14 15 16
8 9 10 11 12 13 14 15 16 17
9 10 11 12 13 14 15 16 17 18
10 11 12 13 14 15 16 17 18 19

madhan ravi
madhan ravi 2019 年 2 月 22 日
Honestly Fangjun Jiang's answer is the way I would do it but according to your question "How can I create this using repmat?" Which I think is a complete waste of time.
repmat(1:10,numel(1:10),1)+repmat((0:9).',1,numel(0:9))
  3 件のコメント
madhan ravi
madhan ravi 2019 年 2 月 22 日
True we should wait:
Another possibilty:
(0:9).' can be replaced with reshape(0:9,1,[])
Sam Thorpe
Sam Thorpe 2019 年 2 月 22 日
Thank you all very much for your help. It is part of a task for a course which is terrible at teaching us the basics of using the software. The use of plus is allowed. It just asks us to use one of the three functions I specified and the colon operator but gave us no method of using them.

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


Jos (10584)
Jos (10584) 2019 年 2 月 22 日
編集済み: Jos (10584) 2019 年 2 月 22 日
I think this is the only acceptable answer is, given all restraints:
A = reshape([1:10 2:11 3:12 4:13 5:14 6:15 7:16 8:17 9:18 10:19],10,10)
which does not use numel, tranpose (.'), semi-colons, and plus :-D
But wait a minute, ... is concatenation using square brackets acceptable or not ...

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by