how do you define a matrix and its variable

I want to create a matrix , by i * j, in matlab, and then let matlab run this. I need to tell matlab what is i and what is j before i define the matrix, right? Then when I define the matrix, can I write aMatrix = [i, j]?
I know how to do matrix like: aMatrix = [1 3 7 ; 5 6 7], but now I want to know how to make a general matrix by giving i and j in matlab.

回答 (1 件)

Adam Danz
Adam Danz 2021 年 11 月 15 日
編集済み: Adam Danz 2021 年 11 月 15 日

0 投票

Learn about preallocation.
Options:
i = 4;
j = 3;
M = nan(i,j)
M = 4×3
NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
M = ones(i,j)
M = 4×3
1 1 1 1 1 1 1 1 1 1 1 1
M = zeros(i,j)
M = 4×3
0 0 0 0 0 0 0 0 0 0 0 0

4 件のコメント

Wenchen Liu
Wenchen Liu 2021 年 11 月 15 日
Hi! can i do like this:
a = -1-i;
b = 1+i;
M = zeros(a,b)
since i want to create a general matrix. But matlab shows me:
Error using zeros
Size vector must be a row vector with real elements.
Error in Assignment (line 3)
M = zeros(a,b)
so I am not sure how to fix the code. Thank you!
Adam Danz
Adam Danz 2021 年 11 月 15 日
編集済み: Adam Danz 2021 年 11 月 15 日
'a' and 'b' are the size of the matrix of 0s. Unless you've defined 'i', i is an imaginary unit. As the error message indicates, 'a' and' 'b' should be real numbers.
Wenchen Liu
Wenchen Liu 2021 年 11 月 15 日
but how could I define i?and should I define it before a and b?
Adam Danz
Adam Danz 2021 年 11 月 15 日
Yes, just like in my demo.

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

カテゴリ

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

タグ

質問済み:

2021 年 11 月 15 日

コメント済み:

2021 年 11 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by