Axioms
Axioms

Reputation: 505

Matlab to Python - Matrix/Array Formation and Operations

I'm working on translating some MATLAB code to Python so I can learn MATLAB better (trying to think in terms of Python first), and I'm a bit stumped on what this block of code is doing.

n = length(a);
S = zeros(n+1,n+1);
S(1,1) = sqrt(1/b(1));
S(2,:) = (S(1,:)-[0,a(1)*S(1,1:end-1)])/sqrt(b(2));
S(3,:) = (S(2,:)-[0,a(2)*S(2,1:end-1)]-[0,0,sqrt(b(2))*S(1,1:end-2)])/sqrt(b(3));
S(4,:) = (S(3,:)-[0,a(3)*S(3,1:end-1)]-[0,0,sqrt(b(3))*S(2,1:end-2)])/sqrt(b(4));

I understand the first 2 lines (create a n+1 by n+1 matrix S), but I'm having trouble understanding the next 3.

From what I understand (n:m) is Matrix lookup notation. So, S(1, 1) means the value at 1st row, first column, which is set to 1/math.sqrt(b[0]) in terms of Python. This would mean that in our matrix S, the first row is an array who 1/math.sqrt(b[0]), and the rest are 0's, right?

For the 4th line, I'm having real trouble understanding the vode. Are we saying the 2nd row is the 1st row minus an array from 0 to a(1)*S(1,1:end-1)? What exactly does a(1)*S(1,1:end-1) represent here?

I see that the next 2 lines is a recurrence relation based on j-1th and j-2th row for some j >= 3 (2 if Python), but I have very little idea as to what the recurrence relation is computing.

Any help "translating" this code to Python (in terms of pseudocode for understanding, not actual hard code) would be tremendously helpful, as learning MATLAB has been pretty tricky so far for me. Thank you.

Upvotes: 2

Views: 121

Answers (2)

Banghua Zhao
Banghua Zhao

Reputation: 1556

For line 3, you are right!

For line 4, a(1)*S(1,1:end-1) means a(1) times a list that consists of the 1st to the last 2nd element of S(1,:). For example, if S(1,:) = [1, 2, 3, 4, 5] then a(1)*S(1,1:end-1) means a(1)*[1, 2, 3, 4]. You can think of end-1 is equivalent to len(S[0]) - 1 in python. The translation of line 4 in python is:

temp = [0] + [a[0]*i for i in S[0][0:-1]]
for i in range(len(S[0])):
   S[1][i] = (S[0][i] - temp[i]) / math.sqrt(b[1])

Based on the 4th line, can you translate 5th and 6th lines?

Upvotes: 2

evantkchong
evantkchong

Reputation: 2606

I'll use some dummy values and try to explain clearly what's going on in the code. Also I'm definitely not an expert in matlab and my knowledge of it is regulated to a first-year programming course in university so do take what I write with a pinch of salt!

Let us define:

a = [1,2,3,4]
b = [1,2,3,4]

Yes, line three S(1,1) = sqrt(1/b(1)); would indeed result in the following array where the value of (0,0) in python is 1 divided by the square root of the first value in list b or math.sqrt(1/b[0]) like you defined.

1 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

Line four S(2,:) = (S(1,:)-[0,a(1)*S(1,1:end-1)])/sqrt(b(2)); has several things going on at once so let's work through it step by step.

  1. S(1,:) refers to the entire 1st row of array S.
  2. a(1)*S(1,1:end-1) is an array where it is the first item of array a(1) = 1 multiplied by the 1st row of array S up till the second last item in the row S(1,1:end-1) = 1 0 0 0.
  3. [0,a(1)*S(1,1:end-1)] is an array where the 1st item is 0 and the 2nd item is the array a(1)*S(1,1:end-1). Hence [0,a(1)*S(1,1:end-1)] = 0 1 0 0 0
  4. S(1,:)-[0,a(1)*S(1,1:end-1)] is just having that entire first row of array S(1,:) = 1 0 0 0 0 subtracted by the array [0,a(1)*S(1,1:end-1)] = 0 1 0 0 0 which gives us 1 -1 0 0 0
  5. Finally we divide everything by sqrt(b(2)) = 1.4142
  6. This gives us S(2,:) = 0.7071 -0.7071 0 0 0

Lines five and six are similar to line four but with some changes to which indexes are being manipulated.

It's not pseudocode like you asked for but I hope my answer helps you to some degree. While you're working on this, you might want to check out this paper which I found rather helpful with regards to understanding arrays in Matlab

MATLAB array manipulation tips and tricks

Upvotes: 3

Related Questions