1) Extract submatrices, 2) vectorize and then 3) put back

后端 未结 1 1981
春和景丽
春和景丽 2021-01-27 01:29

I simplify my problem, let says I have three matrices.

I want to extract the red-boxed sub-matrices. I define

S = [1 4;
     2 5]
1条回答
  •  暖寄归人
    2021-01-27 01:54

    Assuming that your A, B and C matrices have the same dimensions, rather work with a 3D matrix.

    e.g. assuming your example matrices

    M = cat(3,A,B,C)
    

    No to extract those 4 upper left elements:

    M_subset = M(1:2,1:2,:)
    

    And then to reshape them into the vector you had:

    V = M_subset(:)
    

    then manipulate it to get V_new and finally put it back in the original:

    M(1:2,1:2,:) = reshape(V_new,2,2,[])
    

    0 讨论(0)
提交回复
热议问题