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

Click For Summary

Discussion Overview

The discussion revolves around computing the Kronecker product of two arrays in MATLAB, focusing on coding challenges and programming concepts. Participants share their experiences with MATLAB programming, particularly in relation to homework assignments and learning resources.

Discussion Character

  • Homework-related
  • Technical explanation
  • Meta-discussion

Main Points Raised

  • One participant presents a code snippet attempting to compute the Kronecker product but encounters issues with dimension mismatches and looping constructs.
  • Another participant suggests online resources for MATLAB programming to help with general programming difficulties.
  • A participant expresses their struggles with programming concepts, particularly with loops and cell arrays, and mentions the challenges of learning from basic examples.
  • One participant recommends a specific textbook for MATLAB, citing its updates and usefulness in learning the software.
  • There are inquiries about whether the syntax of MATLAB has changed significantly over the past decade, with some participants suggesting that old scripts still function with minor adjustments.
  • Another participant confirms that while some functions may be deprecated, the core language remains stable, and tools are available to assist with script upgrades.

Areas of Agreement / Disagreement

Participants express varying levels of agreement regarding the stability of MATLAB's syntax over the years, with some believing it has remained consistent while others question potential changes.

Contextual Notes

Participants reference specific programming challenges, the need for additional learning resources, and the potential for outdated materials to still be relevant, indicating a variety of assumptions about the learning process and software evolution.

Who May Find This Useful

This discussion may be useful for beginners in MATLAB programming, educators looking for teaching resources, and those interested in the historical context of MATLAB's development.

gfd43tg
Gold Member
Messages
949
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
2K
Replies
43
Views
5K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K