Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
need help

Subject: need help

From: Muhammad Ramzan

Date: 27 Jan, 2013 16:44:07

Message: 1 of 5

Hi everyone,


I have two symbolic matrices;

A = [1 p;0 1];
B = [1 p;0 1-p];

I have to calculate the tensor product of its all permutations as given below (obtained using npermutek function);
i.e. tensor(A,A,A,A) and so on upto tensor(B,B,B,B) below;

>> MAT = npermutek(['A' 'B'],4)

MAT =

AAAA
AAAB
AABA
AABB
ABAA
ABAB
ABBA
ABBB
BAAA
BAAB
BABA
BABB
BBAA
BBAB
BBBA
BBBB

Now how can I pick from MAT the index/matrices AAAA, ......BBBB for tensor product (manually I can do, then the advantage of getting permutations is lost). Could you please help me out.

I shall be really grateful for help.

Regards,

Ramzan.

Subject: need help

From: Nasser M. Abbasi

Date: 27 Jan, 2013 19:11:20

Message: 2 of 5

On 1/27/2013 10:44 AM, Muhammad Ramzan wrote:
> Hi everyone,
>
>
> I have two symbolic matrices;
>
> A = [1 p;0 1];
> B = [1 p;0 1-p];
>
> I have to calculate the tensor product of its all permutations as given below (obtained using npermutek function);
> i.e. tensor(A,A,A,A) and so on upto tensor(B,B,B,B) below;
>
>>> MAT = npermutek(['A' 'B'],4)
>
> MAT =
>
> AAAA
> AAAB
..
> BBAB
>
> Now how can I pick from MAT the index/matrices AAAA, ......BBBB for


long version, feel free to optimse

-------------------------------------
syms p
A = [1 p;0 1];
B = [1 p;0 1-p];

mat = npermutek(['A' 'B'],4);

for i=1:length(mat)
     a=eval(sym(mat(i,1)));
     b=eval(sym(mat(i,2)));
     c=eval(sym(mat(i,3)));
     d=eval(sym(mat(i,3)));
     kron(kron(kron(a,b),c),d)
end
------------------------------------

answer for first row AAAA is

  
[ 1, p, p, p^2, p, p^2, p^2, p^3, p, p^2, p^2, p^3, p^2, p^3, p^3, p^4]
[ 0, 1, 0, p, 0, p, 0, p^2, 0, p, 0, p^2, 0, p^2, 0, p^3]
[ 0, 0, 1, p, 0, 0, p, p^2, 0, 0, p, p^2, 0, 0, p^2, p^3]
[ 0, 0, 0, 1, 0, 0, 0, p, 0, 0, 0, p, 0, 0, 0, p^2]
[ 0, 0, 0, 0, 1, p, p, p^2, 0, 0, 0, 0, p, p^2, p^2, p^3]
[ 0, 0, 0, 0, 0, 1, 0, p, 0, 0, 0, 0, 0, p, 0, p^2]
[ 0, 0, 0, 0, 0, 0, 1, p, 0, 0, 0, 0, 0, 0, p, p^2]
[ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, p]
[ 0, 0, 0, 0, 0, 0, 0, 0, 1, p, p, p^2, p, p^2, p^2, p^3]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, p, 0, p, 0, p^2]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, p, 0, 0, p, p^2]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, p]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, p, p, p^2]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, p]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, p]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
  
--Nasser

Subject: need help

From: Muhammad Ramzan

Date: 28 Jan, 2013 03:38:08

Message: 3 of 5

Your reply reads;

> syms p
> A = [1 p;0 1];
> B = [1 p;0 1-p];
>
> mat = npermutek(['A' 'B'],4);
>
> for i=1:length(mat)
> a=eval(sym(mat(i,1)));
> b=eval(sym(mat(i,2)));
> c=eval(sym(mat(i,3)));
> d=eval(sym(mat(i,3)));
> kron(kron(kron(a,b),c),d)
> end
> ------------------------------------
>
> answer for first row AAAA is
>
>
> [ 1, p, p, p^2, p, p^2, p^2, p^3, p, p^2, p^2, p^3, p^2, p^3, p^3, p^4]
> [ 0, 1, 0, p, 0, p, 0, p^2, 0, p, 0, p^2, 0, p^2, 0, p^3]
> [ 0, 0, 1, p, 0, 0, p, p^2, 0, 0, p, p^2, 0, 0, p^2, p^3]
> [ 0, 0, 0, 1, 0, 0, 0, p, 0, 0, 0, p, 0, 0, 0, p^2]
> [ 0, 0, 0, 0, 1, p, p, p^2, 0, 0, 0, 0, p, p^2, p^2, p^3]
> [ 0, 0, 0, 0, 0, 1, 0, p, 0, 0, 0, 0, 0, p, 0, p^2]
> [ 0, 0, 0, 0, 0, 0, 1, p, 0, 0, 0, 0, 0, 0, p, p^2]
> [ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, p]
> [ 0, 0, 0, 0, 0, 0, 0, 0, 1, p, p, p^2, p, p^2, p^2, p^3]
> [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, p, 0, p, 0, p^2]
> [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, p, 0, 0, p, p^2]
> [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, p]
> [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, p, p, p^2]
> [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, p]
> [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, p]
> [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
>
> --Nasser



Kindly clarify

 d=eval(sym(mat(i,3)));
should be d=eval(sym(mat(i,4))); or not?

Regards,
Ramzan.

Subject: need help

From: Muhammad Ramzan

Date: 28 Jan, 2013 03:56:08

Message: 4 of 5

Your reply reads;

> syms p
> A = [1 p;0 1];
> B = [1 p;0 1-p];
>
> mat = npermutek(['A' 'B'],4);
>
> for i=1:length(mat)
> a=eval(sym(mat(i,1)));
> b=eval(sym(mat(i,2)));
> c=eval(sym(mat(i,3)));
> d=eval(sym(mat(i,3)));
> kron(kron(kron(a,b),c),d)
> end
> ------------------------------------
>
> answer for first row AAAA is
>
>
> [ 1, p, p, p^2, p, p^2, p^2, p^3, p, p^2, p^2, p^3, p^2, p^3, p^3, p^4]
> [ 0, 1, 0, p, 0, p, 0, p^2, 0, p, 0, p^2, 0, p^2, 0, p^3]
> [ 0, 0, 1, p, 0, 0, p, p^2, 0, 0, p, p^2, 0, 0, p^2, p^3]
> [ 0, 0, 0, 1, 0, 0, 0, p, 0, 0, 0, p, 0, 0, 0, p^2]
> [ 0, 0, 0, 0, 1, p, p, p^2, 0, 0, 0, 0, p, p^2, p^2, p^3]
> [ 0, 0, 0, 0, 0, 1, 0, p, 0, 0, 0, 0, 0, p, 0, p^2]
> [ 0, 0, 0, 0, 0, 0, 1, p, 0, 0, 0, 0, 0, 0, p, p^2]
> [ 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, p]
> [ 0, 0, 0, 0, 0, 0, 0, 0, 1, p, p, p^2, p, p^2, p^2, p^3]
> [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, p, 0, p, 0, p^2]
> [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, p, 0, 0, p, p^2]
> [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, p]
> [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, p, p, p^2]
> [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, p]
> [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, p]
> [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]
>
> --Nasser



Kindly clarify and help further

1. d=eval(sym(mat(i,3))); whether it should be d=eval(sym(mat(i,4))); or not?

2. I have to further use these 16 matrices, generated by the code you have supplied through kron(kron(kron(a,b),c),d), for multiplication with another 16x16 matrix, say the first one you have given in your reply and to sum up the result.
please help also for it.
i.e. tensor(A,A,A,A).(16x16 matrix).ctranspose(tensor(A,A,A,A))+.......+tensor(B,B,B,B).(16x16 matrix).ctranspose(tensor(B,B,B,B))

where tensor(A,A,A,A) means the first 16x16 matrix generated by your code and given by you above.

Regards,
Ramzan.

Subject: need help

From: Nasser M. Abbasi

Date: 28 Jan, 2013 12:47:58

Message: 5 of 5

On 1/27/2013 9:56 PM, Muhammad Ramzan wrote:
> Your reply reads;
>
>> syms p
>> A = [1 p;0 1];
>> B = [1 p;0 1-p];
>>
>> mat = npermutek(['A' 'B'],4);
>>
>> for i=1:length(mat)
>> a=eval(sym(mat(i,1)));
>> b=eval(sym(mat(i,2)));
>> c=eval(sym(mat(i,3)));
>> d=eval(sym(mat(i,3)));
>> kron(kron(kron(a,b),c),d)
>> end
>> ------------------------------------
>>


>
>
> Kindly clarify and help further
>
> 1. d=eval(sym(mat(i,3))); whether it should be d=eval(sym(mat(i,4))); or not?

yes ofocurse. It was just a typo. It is clear it should have been mat(i,4).

>
> 2. I have to further use these 16 matrices, generated by the code
>you have supplied through kron(kron(kron(a,b),c),d), for multiplication with
>another 16x16 matrix, say the first one you have given in your reply and to sum up the result.
> please help also for it.
> i.e. tensor(A,A,A,A).(16x16 matrix).ctranspose(tensor(A,A,A,A))+.......+
>tensor(B,B,B,B).(16x16 matrix).ctranspose(tensor(B,B,B,B))
>
> where tensor(A,A,A,A) means the first 16x16 matrix generated by your code and given by you above.
>

I do not have time now. But why do not you try to work it out.
I suggest doing one step at a time. You are working in symbolics,
so you need to stick to Matlab functions that supports symbolics.

Take the result from one row, and do the operations above on it.
Add the code in the loop shown above.

Sorry, I have a HW to finish myself and no time.

good luck
--Nasser

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us