0

I am working on matlab conversion code to python.
I do not understand these 2 lines of code.

d1 = s(1:3,2) - s(1:3,1);
d2 = s(1:3,end) - s(1:3,end-1);

what these 2 lines are doing? can anyone explain me?
and how will i convert this in python?

6
  • Have tried printing the results by parts in MATLAB? What did you get? Commented Jan 6, 2014 at 5:40
  • i do not have matlab but I need to do conversion Commented Jan 6, 2014 at 5:41
  • 2
    Try this to run MATLAB code online. Commented Jan 6, 2014 at 5:42
  • 1
    Try this answer in another SO post. Commented Jan 6, 2014 at 6:06
  • 2
    Also have a look at this: wiki.scipy.org/NumPy_for_Matlab_Users Commented Jan 6, 2014 at 6:22

3 Answers 3

3

In Matlab:

% Matlab
s = [1,2,3,4,5; ...
    6,7,8,9,10; ...
    11,12,13,14,15];
d1 = s(1:3,2) - s(1:3,1);
d2 = s(1:3,end) - s(1:3,end-1);

disp('--s--')
disp(s)
disp('--d1--')
disp(d1)
disp('--d2--')
disp(d2)

disp('--s(1:3,2)--')
disp(s(1:3,2))
disp('--s(1:3,1)--')
disp(s(1:3,1))

disp('--s(1:3,end)--')
disp(s(1:3,end))
disp('--s(1:3,end-1)--')
disp(s(1:3,end-1))

can be translated to Python as:

# Python
import numpy as np

s = np.asarray([ \
    1,2,3,4,5, \
    6,7,8,9,10, \
    11,12,13,14,15]).reshape(3,-1);
d1 = s[0:3,1] - s[0:3,0];
d2 = s[0:3,-1] - s[:3,-2];

print '--s--'
print s
print '--d1--'
print d1
print '--d2--'
print d2

print '--s[0:3,1]--'
print s[0:3,1]
print '--s[0:3,0]--'
print s[0:3,0]

print '--s[0:3,-1]--'
print s[0:3,-1]
print '--s[0:3,-2]--'
print s[0:3,-2]

Results of Matlab:

--s--
    1    2    3    4    5
    6    7    8    9   10
   11   12   13   14   15
--d1--
   1
   1
   1
--d2--
   1
   1
   1
--s(1:3,2)--
    2
    7
   12
--s(1:3,1)--
    1
    6
   11
--s(1:3,end)--
    5
   10
   15
--s(1:3,end-1)--
    4
    9
   14

vs of Python:

--s--
[[ 1  2  3  4  5]
 [ 6  7  8  9 10]
 [11 12 13 14 15]]
--d1--
[1 1 1]
--d2--
[1 1 1]
--s[0:3,1]--
[ 2  7 12]
--s[0:3,0]--
[ 1  6 11]
--s[0:3,-1]--
[ 5 10 15]
--s[0:3,-2]--
[ 4  9 14]

They match.

Evaluate Matlab code online /here/ and Python code /here/, /here/ and with packages /here/.

Sign up to request clarification or add additional context in comments.

3 Comments

I'd probably prefer np.array([[1,2,3,4,5], [6,7,8,9,10], [11,12,13,14,15]]) there - note that you don't need \ to continue a line within {[(
@Eric Your point is totally correct. We just tried to keep Python translation similar to Matlab code as much as possible.
Also, you really shouldn't use semicolons in python
2

s is a matrix. s(1:3,2) creates a vector from rows 1 to 3 and column 2. s(1:3,1) creates a vector from rows 1 to 3 and column 1. d1 is then another vector being the element by element difference

end is special syntax for the last row or column. end-1 is the penultimate row/column

Comments

1

d1: the difference between 2nd column and 1st column of the first 3 rows of s

d2: the difference between last column and the one before last column of the first 3 rows of s

You can test it online

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.