function that combines sub matrices into one matrix

I am trying to write a function that takes as its input arguments two scalar positive integers named n and m in that order. The function returns Q, a 2n-by-2m matrix. Q consists of four n-by-m submatrices. The elements of the submatrix in the top left corner are all 0s, the elements of the submatrix at the top right are 1s, the elements in the bottom left are 2s, and the elements in the bottom right are 3s. So far I have a1 = zeros(2,3) a2 = ones(2,3); a3 = 2*ones(2,3) a4= 3*ones(2,3) A = [a1, a2; a3, a4] I don't know how to create a function from what I have.

 採用された回答

Birdman
Birdman 2017 年 11 月 20 日

1 投票

function y=A(n,m)
a1=zeros(n,m);a2=ones(n,m);a3=2*ones(n,m);a4=3*ones(n,m);
y=[a1 a2;a3 a4];
end
From command line, write
A(2,3)

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2017 年 11 月 20 日
編集済み: Andrei Bobrov 2017 年 11 月 20 日

1 投票

>> your_function = @(m,n)repelem([0, 1; 2, 3],m,n)
your_function =
function_handle with value:
@(m,n)repelem([0,1;2,3],m,n)
>> your_function(2,3)
ans =
0 0 0 1 1 1
0 0 0 1 1 1
2 2 2 3 3 3
2 2 2 3 3 3
>>
for older MATLAB:
your_function = @(m,n)kron([0, 1; 2, 3],ones(m,n))

カテゴリ

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

質問済み:

2017 年 11 月 20 日

編集済み:

2017 年 11 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by