質問

How do you translate matrix of A [(N) x (N)] to matrix B [(2N) x (2N)], such that:

if A(i,j)>=0, then:
B(i,j) = [ A(i,j) 0
           0      A(i,j)]

if A(i,j)<0, then:
B(i,j) = [ 0       A(i,j)
           A(i,j)  0    ]

?

For example1 by:

1  2
3  4

I'm want to get:

1 0 2 0
0 1 0 2
3 0 4 0
0 3 0 4

For example2 by:

1  -2
3  -4

I'm want to get:

1 0 0 2
0 1 2 0
3 0 0 4
0 3 4 0
役に立ちましたか?

解決

Use the Kronecker tensor product:

B = kron(A.*(A>=0), [1 0; 0 1]) + kron(A.*(A<0), [0 1; 1 0]);

Or maybe

B = kron(A.*(A>=0), [1 0; 0 1]) - kron(A.*(A<0), [0 1; 1 0]);

if you want all positive values (your examples and your original formulation don't agree on this)

他のヒント

very simple using logical conditions:

B=[A.*(A>=0), A.*(A<0) ; A.*(A<0), A.*(A>=0)];

for example,

A=[1 2 ; -3 4];

B =

 1     2     0     0
 0     4    -3     0
 0     0     1     2
-3     0     0     4

Postscipt:

This answer was written to answer the question above in its initial forms:

How do you translate matrix of A [(N) x (N)] to matrix B [(2N) x (2N)], such that:

if A(i,j)>=0, then:
B(i,j) = [ A(i,j) 0
           0      A(i,j)]

if A(i,j)<0, then:
B(i,j) = [ 0       A(i,j)
           A(i,j)  0    ]

?

later the OP wrote down some examples that made clear what he\she were after.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top