フィルターのクリア

2d array in matlab

5 ビュー (過去 30 日間)
Rakesh Singh
Rakesh Singh 2014 年 9 月 20 日
編集済み: dpb 2014 年 9 月 21 日
how to declare 2d array in matlab

回答 (1 件)

dpb
dpb 2014 年 9 月 20 日
Matlab does silent automagic allocation -- just assign
z=zeros(2); % is a 2x2 array
See "Getting Started" section in the documentation and work thru the examples to get an idea on "how Matlab works.
  2 件のコメント
Image Analyst
Image Analyst 2014 年 9 月 20 日
編集済み: Image Analyst 2014 年 9 月 20 日
A neat trick is that you can expand an existing array or scalar by setting the last value to something. For example:
m=5; % m is a scalar
m(10,20) = 9;
m is now a 10 by 20 array of zeros except for the two (1,1) and (10,20) elements which equal 5 and 9 respectively.
You can also preallocate cell arrays with the cell() function and structure arrays with the struct() function.
dpb
dpb 2014 年 9 月 20 日
編集済み: dpb 2014 年 9 月 21 日
...can expand an existing array or scalar by setting the last value to something.
I was just coming back to add that in, IA... :)
But, I'll note for the OP one doesn't have to add to an existing variable, same thing works to create the array; ie,
m(10,20)=9;
as the first statement will leave the array w/ just the one "9" at location 10,10, all else is zeros. It's functionally equivalent to
m=zeros(10,20);
m(end,end)=9;
where I've deliberately used the alternate functional indexing reference end as a tutorial device as well... :)

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by