November 4, 2005
list partterns in Matlab and Python
list partterns in Matlab and Python
A comparation between usage in Matlab against Python of the List variable as the most common and frequently utility in the languages.
Indexing
Key point of Indexing: Index, List to be filtered
Matlab
lst =
0.4103 0.0579 0.8132
0.8936 0.3529 0.0099
>> i = lst > 0.5 %@ the Index, a list with integer elements
i =
0 0 1
1 0 0
>> lst(i) %@ the Result, get a new list from lst with elements indexed by i
ans =
0.8936
0.8132
>> lst(lst>0.5)=1
lst =
1.0000 0.0579 1.0000
1.0000 1.0000 0.0099
Append
Python
Matlab
No declaration or additional appending operation.
>> lst(2) = 1
lst =
0 1
>> lst(5) = 1
lst =
0 1 0 0 1
>> lst(length(lst)+1) = 9 %@ Appending operation
lst =
0 1 0 0 1 9
Sorting
lst =
0.9501 0.4860 0.4565 0.4447
0.2311 0.8913 0.0185 0.6154
0.6068 0.7621 0.8214 0.7919
>> sort(lst) %@ Matrix sorting means “Sorts each column”.
ans =
0.2311 0.4860 0.0185 0.4447
0.6068 0.7621 0.4565 0.6154
0.9501 0.8913 0.8214 0.7919
>> sortrows(lst,1) %@ Each row treated as a element in List.
ans =
0.2311 0.8913 0.0185 0.6154
0.6068 0.7621 0.8214 0.7919
0.9501 0.4860 0.4565 0.4447
Filed by
charlie
at 10:19 am under Coding, Python, Uncategorized
