Help in creating Matrix

I want to create a function called LMatrix that generates the following output given three inputs,A,B,and i,where A is an n x n matrix,B is an n x m matrix,and i is a positive integer.
*Output *
Code
function L= LMatrix (A,B.i)
[ar ac] = size(A);
[br bc] = size(B);
[lr lc] = size(L);
if nargin < 4
r = 1; c = 1;
else
r = round(r); c = round(c);
end
L = [A,B];
rr = r + br - 1;
cc = c + bc - 1;
out(r:rr,c:cc) = L;
Test Cases
>> A = 2; B = 1; i = 3;
>> L1 = LMatrix(A,B,i)
L1 =
100 210 421
>> A = [1 4; 2 3]; B = [2; 1]; i = 3;
>> L2 = LMatrix(A,B,i)
L2 =
200 100 620 710
34 6 2 33 7 1

1 Comment

are you sure you have the right output in your function declaration?

Sign in to comment.

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 19 Oct 2012
Edited: Azzi Abdelmalek on 19 Oct 2012
A=magic(2);B=[1; 2];
n=5
p=cell2mat(arrayfun(@(x) [zeros(x,1) ;(1:n-x)'] ,(1:n)','un',0)')
C=cell2mat(arrayfun(@(x) [zeros(x-1,1) ;ones(n-x+1,1)] ,(1:n)','un',0)')
out=cell2mat(cellfun(@(x,y) A^x*B*y,num2cell(p),num2cell(C),'un',0))

Categories

Asked:

on 19 Oct 2012

Community Treasure Hunt

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

Start Hunting!