MATLAB Matrice Function Question help please?

1 回表示 (過去 30 日間)
Reelz
Reelz 2012 年 4 月 5 日
I am trying to write a matlab program which creates an n by n matrix with the following:
a = [0 1 2 3 4; 1 0 1 2 3; 2 1 0 1 2; 3 2 1 0 1; 4 3 2 1 0]
How would I go about doing this?
42 minutes ago
- 4 days left to answer.
Additional Details Also looking like this:
a =
0 1 2 3 4
1 0 1 2 3
2 1 0 1 2
3 2 1 0 1
4 3 2 1 0
Some detailed instruction/help would really help me in clarifying how to program matrices in general.

採用された回答

Kye Taylor
Kye Taylor 2012 年 4 月 5 日
It's a toeplitz matrix... so use the command
toeplitz(0:4)
to recreate your example. I'm sure you can extrapolate to the general n-by-n case.
More generally, if you want to create a matrix, use an array creation function like toeplitz, ones, rand, (type
doc elmat
and check out 1.) the list of Specialized matrices at the bottom of the help file and 2.) the list of elementary matrices at the top of the file.)
Beyond array creation functions, you can concatenate values both horizontally and vertically to create a vector/matrix/high d array. One way to concatenate is with square braces. For example, to concatenate 1 and 2 horizontally, use the command
v = [1,2]
That is, commas in square brackets can be interpreted as horizontal concatenation. To concatenate vertically, use the command
v = [1;2]
That is, semicolons in square brackets can be interpreted as vertical concatenation.
To build a matrix, construct it row-by-row sequentially from the first row to the last row. For example, the command
a = [0,1,2,3,4;1,0,1,2,3]
constructs the first row (0,1,2,3), then concatenates vertically (;) the second row (1,0,1,2,3);
That may not be the clarification you were looking for, so let me know if I can clarify.
  2 件のコメント
Reelz
Reelz 2012 年 4 月 6 日
****** I am specifically looking how to do this in the editor window. where we start off function = ( )
Your answer did help me, I am just confused on using the editor window. Help in that area would be awesome!
Kye Taylor
Kye Taylor 2012 年 4 月 7 日
I like Image Analysts approach below... you could simplify with just
function m = makeToeplitz(n)
m = toeplitz(0:n);

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2012 年 4 月 6 日
For example, write this in the editor window (Use File->New if you have to, or click the blank page icon to get a new editor window):
function m = test1(n)
if nargin >= 1
m = toeplitz(0:n);
else
m = toeplitz(0:4); % Default
warningMessage = sprintf('Using default n of 4');
uiwait(warndlg(warningMessage));
end
and save it as test1.m. Then you can run it without any args and it will use a default of 4, or you can say
>> test1(5)
and it will use an n of 5.
  3 件のコメント
Walter Roberson
Walter Roberson 2012 年 4 月 7 日
You have accidentally saved a file under the name toeplitz.m . Remove your file with that name.
Kye Taylor
Kye Taylor 2012 年 4 月 7 日
Impressive debugging :)

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


Andrei Bobrov
Andrei Bobrov 2012 年 4 月 7 日
out = abs(bsxfun(@minus,0:4,(0:4)'))

カテゴリ

Help Center および File ExchangeElementary Math についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by