HP Forums

Full Version: Blockmatrix
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Blockmat(2,3, {A,B,C,D,E,F})--> [A B C; D E F]
Blockmat(3,2, {A,B,C,D,E,F})--> [A B; C D; E F]
Blockmat(1,6, {A,B,C,D,E,F})--> [A B C D E F]
A, B, C, D, E, F are matrices of equal dimensions.
Code:

#cas
Blockmat(n,p,lst):=
BEGIN
local j,k,c,d,m3,m2;
IF ((n*p)<>size(lst)) THEN RETURN("matrices sizes"); END
m2:=[]; m3:=[]; L0:={};
if p==1 then Blockmat1();return(m2); end;
for k from 1 to n do
IF k==1 THEN 
for j from 1 to p do
L0(j):=lst(j);
end;
m2:=trn(concat(L0));
c:=p; 
ELSE
L0:={};
d:=1;
for j from c+1 to k*p do
L0(d):=lst(j);
d:=d+1;
end;
c:=k*p;
m3:=trn(concat(L0));
m2:=concat(m2,m3);
END;
end;
return(trn(m2));
END;
#end


#cas
Blockmat1()
BEGIN
LOCAL j;
L0:={};
B:=0;
[A,B]:=DIM(lst(1));
for j from 1 to (size(lst)-1) do
IF (DIM((lst(j)))<>DIM((lst(j+1)))) THEN kill; END;
end;
for j from 1 to (size(lst)) do
L0:=concat(L0,mat2list(lst(j)));
end;
IF B==0 THEN m2:=(list2mat(L0,colDim(lst(1)))); ELSE m2:=(list2mat(L0,B)); END;
END;
#end
Isn't there already a command that does this (list2mat)?
if matrix A= [1 2; 3 4] and B =[5 6; 7 8]
to block A and B into 2*1 is C=[A;B]=[1 2; 3 4;5 6;7 8]
list2mat will block it to C=[[A],[B]]=[[1 2; 3 4];[5 6; 7 8]] with extra brackets

if you have matrices A,B,C,D to be block into 2*2 matrix
A,B need to augmented, C ,D need to augmented---> CONCAT in hp prime
then the first has to stack on the later---> no STACK in hp prime;
that's what my code does...
I see now; this little program is quite useful.
Reference URL's