フィルターのクリア

I want to define a square in a matrix

1 回表示 (過去 30 日間)
Kaelyn
Kaelyn 2013 年 10 月 16 日
編集済み: dpb 2013 年 10 月 16 日
I want to define a square located at i=1 to a certain distance down and across. The matrix I have so far is
n=10;
A=zeros(n,n);
for i=1:n*n
A(1+i)=i;
end
My square starts at i=1 and four down and four across I would like to make the other area have a value of 0. I need to change the size of the square when n changes.
  1 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 16 日
Can you post the expected result

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

採用された回答

dpb
dpb 2013 年 10 月 16 日
編集済み: dpb 2013 年 10 月 16 日
>> n=6; % overall size
s=4; % size of square
r=1; c=1; % row,column upper left corner
A=zeros(n);
A(r:r+s-1,c:c+s-1)=reshape(1:s*s,s,[]);
>> A
A =
1 5 9 13 0 0
2 6 10 14 0 0
3 7 11 15 0 0
4 8 12 16 0 0
0 0 0 0 0 0
0 0 0 0 0 0
>>
Salt to suit...for your example, r=c=1 of course.
You'll want error-checking, etc., etc., etc., ...

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by