# Kronecker Product MATLAB

1. Jun 21, 2014

### Maylis

1. The problem statement, all variables and given/known data
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:

2. Relevant equations

3. The attempt at a solution
Code (Text):
A = [1 2; 3 4];
B = [1 10 100];
Here is my code and result
Code (Text):
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 (Text):
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 (Text):
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 (Text):
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

2. Jun 21, 2014

### Staff: Mentor

3. Jun 21, 2014

### Maylis

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. Jun 22, 2014

### Staff: Mentor

I felt that this book was way better than all the rest:

https://www.amazon.com/Essential-MA...ie=UTF8&qid=1403422990&sr=1-3&keywords=matlab

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: May 6, 2017
5. Jun 22, 2014

### Maylis

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. Jun 22, 2014

### Staff: Mentor

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.