Block matrix indexing

Hello this might be a stupid question but i need some help indexing a Matlab matrix consisting of several sub-matrices.

for k = 1:tf-1
    
    r(k) = rand(1)/4;
    u(k+1) = 0.5;
    
    x1(k+1) = A(1,1)*x1(k) + A(1,2)*x2(k) + B(1,1)*u(k);
    x2(k+1) = A(2,1)*x1(k) + A(2,2)*x2(k) + B(2,1)*u(k);
    
    x = [x1(k) x2(k)]';
    
    y(k) = C*x + r(k);
    
    P_prior(k+1) = A*P(k)*A.' + Q;
    
    K(k+1) = P_prior(k+1)*C.'/(C*P_prior(k+1)*C.' + R);
    xhat(k+1) = x(k+1) + K(k+1)*(y(k) - C*x(k+1));
    P(k+1) = (eye(size(1,1)) - K(k+1)*C)*P_prior(k+1);
    

end

For example i want to populate P_prior with several 2x2 matrices and the k index to stand for 1st block matrix, second, etc. Is there even a way to do this with Matlab or do i have to come around another way?

Thank you in advance.

Topic matrix matlab indexing

Category Data Science


If you can predefine the shape of P_prior, something like this might work:

P_prior=zeros(2,2,2,2) //which can be interpreted as a 2X2 matrix of 2X2 matrices
P_prior(:,:,1,1)=ones(2,2)
P_prior(:,:,1,2)=ones(2,2)
P_prior(:,:,2,1)=ones(2,2)
P_prior(:,:,2,2)=ones(2,2)

or the shape of P_prior is more like

P_prior=zeros(4,4)

then this would overwrite the upper left sub-matrix

P_prior(1:2,1:2)=ones(2,2)

more general

for i=1:2
  for j=1:2
    P_prior(2*i-1:2*i,2*j-1:2*j)=ones(2,2)
  end
end

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.