Compute Kronecker Product of Two Arrays in MATLAB - Step-by-Step Guide

In summary: Its no where near as popular as open source languages like python, java, and perl so when it does change its a big deal and everyone knows about it.In summary, the conversation revolves around a problem that requires writing code to compute the Kronecker product of two arrays. The conversation includes the dimensions and definition of the Kronecker product, as well as a code attempt and resulting output. The individual is struggling with incorporating the code into loops and expresses difficulty in understanding programming in general. Recommendations for resources and advice are also provided.
  • #1
gfd43tg
Gold Member
950
50

Homework Statement


In this problem, you will write code that computes the Kronecker product of two arrays. Suppose
A is a numeric array of size r-by-c and B is a numeric array of size n-by-m. Then the Kronecker
product of A with B is a numeric array, of dimension rn-by-cm, defined as:

Homework Equations


The Attempt at a Solution


Code:
A = [1 2; 3 4];
B = [1 10 100];

Here is my code and result
Code:
C = zeros(length(A),length(B)*length(A));
for i = 1:length(A) 
   for j = 1:length(C)
       C(i,1:3) = A(i)*B
   end
end

Code:
C =

     1    10   100     0     0     0
     3    30   300     0     0     0

I cannot for the life of my figure out how to add onto columns 4-6 for this array. It seems like my for loop with j doesn't do anything, and everytime I mess with my columns I get mismatch dimension errors.

C should look like this
Code:
C =

     1    10   100     2    20   200
     3    30   300     4    40   400

I am able to construct this array in the command window, but I cannot generalize to get it working with my loops. I want to take the code I just did here and incorporate it into my loops, but it is proving to be very difficult.

Code:
zeros(length(A),length(B)*length(A))

ans =

     0     0     0     0     0     0
     0     0     0     0     0     0

EDU>> C(1,1:3) = A(1)*B;
EDU>> C(2,1:3) = A(2)*B;
EDU>> C(1,4:6) = A(3)*B;
EDU>> C(2,4:6) = A(4)*B;
EDU>> C

C =

     1    10   100     2    20   200
     3    30   300     4    40   400
 
Physics news on Phys.org
  • #3
Yes. I started programming about 2 weeks ago. It doesn't help that I needed to use a cell array without ever being taught what it is. Last week I needed to use a loop for a homework question, and this week we are learning loops. I am struggling a lot with programming, so I will look into it. I bought a textbook to help me, it has been helpful, but the homework problems are not trivial for a first time learner, especially when the examples in the lecture videos are extremely basic. These require a lot of thought and I can do one problem for hours without making any significant progress.
 
  • #4
I felt that this book was way better than all the rest:

https://www.amazon.com/dp/0123943981/?tag=pfamazon01-20

They keep updating it too for each version of MATLAB.

I used MATLAB once in a course on computational physics to do FFT analysis. The rest of the time we used the Open Source Physics framework to do our simulations. The instructor wanted us to be aware of its power and be a aware that there are free versions of it like freemat and octave. Personally I like freemat, it supports the core language only none of the specialized MATLAB packages, A lot of engineers out in industry rely on MATLAB and its many packages.

Anyway, hang in there, MATLAB is a good skill to have even if its not the best programming language around.
 
Last edited by a moderator:
  • #5
has the synthax of MATLAB changed significantly in the last 10 years, i.e. an introductory course from 2002 would be the same as one today? I can see if maybe advanced functions have changed. Just to see if I can buy an old edition
 
  • #6
Maylis said:
has the synthax of MATLAB changed significantly in the last 10 years, i.e. an introductory course from 2002 would be the same as one today? I can see if maybe advanced functions have changed. Just to see if I can buy an old edition

No I dont' think so. Its a big selling point that old scripts still run with minor changes needed if at all. They might deprecate some functions but again I think it would be rare and also they probably would supply a tool to upgrade scripts if necessary as they have quite a few users.

http://en.wikipedia.org/wiki/MATLAB

There's alist of versions with comments describing what changed for some and no language chages are mentioned except for the addition of Object-Oriented programming support.

Its a very stable and vendor-specific platform.
 

Related to Compute Kronecker Product of Two Arrays in MATLAB - Step-by-Step Guide

What is the Kronecker Product in MATLAB?

The Kronecker Product in MATLAB is a mathematical operation that combines two matrices to create a new matrix. It is denoted by the symbol "⊗" and is used to represent the outer product of two vectors or the tensor product of two matrices.

How is the Kronecker Product calculated in MATLAB?

The Kronecker Product in MATLAB can be calculated using the "kron" function. This function takes in two matrices as inputs and returns the Kronecker Product as a new matrix. It follows the rule of multiplying each element of the first matrix with the entire second matrix.

What are the applications of the Kronecker Product in MATLAB?

The Kronecker Product in MATLAB has various applications in linear algebra, signal processing, and image processing. It is commonly used to create block matrices, perform tensor operations, and solve systems of equations.

Is the Kronecker Product commutative in MATLAB?

No, the Kronecker Product is not commutative in MATLAB. This means that the order of the matrices matters when calculating the Kronecker Product. In other words, A ⊗ B is not equal to B ⊗ A in MATLAB.

Can the Kronecker Product be used with non-square matrices in MATLAB?

Yes, the Kronecker Product can be used with non-square matrices in MATLAB. The resulting matrix will have dimensions equal to the product of the dimensions of the two input matrices. For example, if A is a m x n matrix and B is a p x q matrix, then A ⊗ B will be a mp x nq matrix.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
2
Replies
43
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
841
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
929
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
Back
Top