开发者

Is there any function in MATLAB for changing the form of a matrix?

开发者 https://www.devze.com 2022-12-28 20:22 出处:网络
I have to get the unknown matrix by changing the form of a known matrix considering the following rules:

I have to get the unknown matrix by changing the form of a known matrix considering the following rules:

开发者_运维百科
H = [-P'|I] %'
G = [I|P]

where

  • H is a known matrix
  • G is an unknown matrix which has to be calculated
  • I is the identity matrix

So for example, if we had a matrix,

H = [1 1 1 1 0 0; 
     0 0 1 1 0 1; 
     1 0 0 1 1 0]

its form has to be changed to

H = [1 1 1 1 0 0; 
     0 1 1 0 1 0; 
     1 1 0 0 0 1]

So

-P' = [1 1 1; 
       0 1 0; 
       1 1 0] 

and in case of binary matrices -P = P.

Therefore

 G = [1 0 0 1 1 1; 
      0 1 0 0 1 0; 
      0 0 1 1 1 0]

I know how to solve it on paper by performing basic row operations but haven't figured out how to solve it using MATLAB yet.

What is the method for solving the given problem?


If the order of columns in -P' doesn't matter, here's one solution using the function ISMEMBER:

>> H = [1 1 1 1 0 0; 0 0 1 1 0 1; 1 0 0 1 1 0];  %# From above
>> pColumns = ~ismember(H',eye(3),'rows')  %'# Find indices of columns that
                                            %#   are not equal to rows
pColumns =                                  %#   of the identity matrix

     1
     0
     1
     1
     0
     0

>> P = -H(:,pColumns)'  %'# Find P

P =

    -1     0    -1
    -1    -1     0
    -1    -1    -1

>> G = logical([eye(3) P])  %# Create the binary matrix G

G =

     1     0     0     1     0     1
     0     1     0     1     1     0
     0     0     1     1     1     1

NOTE: This solution will work properly for integer or binary values in H. If H has floating-point values, you will likely run into an issue with floating-point comparisons when using ISMEMBER (see here and here for more discussion of this issue).

0

精彩评论

暂无评论...
验证码 换一张
取 消