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?
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?
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/.
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