MATLAB: How to combine a matrix that has elements up to n

cell arraysMATLABmatricesmatrixmatrix arraymatrix manipulation

So, I have this pre calculate matrixes of A that are 4×4 and they are all positioned to be along the diagonal I then have matrix r that are 4×1, set up in a similar pattern but begin with 2. Now, I have set this matrix to go up to 4 calculated values but I need it to completed this pattern for a number of n=1:ntotal, where ntotal is any number. The BA matrix will always be something like (ntotal*4)X(ntotal*5) while by BR will be something like (ntotal*4)X(ntotal-1).
clear; clc;
nspan=4;
EI=[200,600,600,200];
L=[10,20,20,10];
%%constants setup
a=zeros(1,nspan);
b=a;c=a; d=a;
for n=1:nspan
a(n)=L(n);
b(n)=L(n)/EI(n);
c(n)=L(n)^2/(2*EI(n));
d(n)=L(n)^3/(6*EI(n));
end
%%Compute A to the n
for n = 1:nspan
A{n} = [1,0,0,0;a(n),1,0,0;c(n),b(n),1,0;-d(n),-c(n),-a(n),1];
end
for n = 1:nspan
r{n} = [1;a(n);c(n);-d(n)];
end
I=eye(4);
Z = zeros(4,4);
Z1 = zeros(4,1);
BA=[A{1},-I,Z,Z,Z;...
Z,A{2},-I,Z,Z;...
Z,Z,A{3},-I,Z;...
Z,Z,Z,A{4},-I];
BR=[Z1,Z1,Z1;...
-r{2},Z1,Z1;...
Z1,-r{3},Z1;...
Z1,Z1,-r{4}];
B=[BA,BR];

Best Answer

This code should do the trick. I also slightly modified some earlier code.
nspan=4;
EI=[200,600,600,200];
L=[10,20,20,10];
%%constants setup
L=L(:)';%force direction

EI=EI(:)';%force direction
a=L;
b=L./(n);
c=L.^2./(2*EI);
d=L.^3./(6*EI);
%%Compute A to the n
A=cell(1,nspan);r=cell(1,nspan);
for n = 1:nspan
A{n} = [1,0,0,0;a(n),1,0,0;c(n),b(n),1,0;-d(n),-c(n),-a(n),1];
r{n} = [1;a(n);c(n);-d(n)];
end
I=eye(4);
Z = zeros(4,4);
Z1 = zeros(4,1);
%pre-allocate BA as a cell to make indexing easier

BA=repmat({Z},nspan,nspan+1);
BA((nspan+1)*(1:nspan))={-I};
BA(((nspan+1)*(1:nspan))-nspan)=A;
BA=cell2mat(BA);
% BA=[A{1},-I,Z,Z,Z;...
% Z,A{2},-I,Z,Z;...
% Z,Z,A{3},-I,Z;...
% Z,Z,Z,A{4},-I];
%pre-allocate BA as a cell to make indexing easier
BR=repmat({Z1},nspan,nspan-1);
BR(2:(nspan+1):end)=cellfun(@(x) -x,r(2:end),'UniformOutput',false);
BR=cell2mat(BR);
% BR=[Z1,Z1,Z1;...
% -r{2},Z1,Z1;...
% Z1,-r{3},Z1;...
% Z1,Z1,-r{4}];
B=[BA,BR];
Related Question