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

Click For Summary
The discussion focuses on computing the Kronecker product of two arrays in MATLAB, specifically addressing issues with coding loops to achieve the desired output format. The user successfully generates part of the Kronecker product but struggles with expanding the result to include additional columns. Suggestions include utilizing online resources for MATLAB programming and acknowledging the challenges faced by beginners. The conversation also touches on the stability of MATLAB's syntax over the years, indicating that older scripts generally remain compatible with newer versions. Overall, the thread highlights the learning curve associated with MATLAB programming and the importance of persistence in mastering it.
gfd43tg
Gold Member
Messages
947
Reaction score
48

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
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.
 
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:
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
 
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.
 

Similar threads

  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
1K
Replies
43
Views
5K
  • · Replies 4 ·
Replies
4
Views
713
  • · Replies 1 ·
Replies
1
Views
793
  • · Replies 3 ·
Replies
3
Views
1K
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K