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 = rand(2,3) %@ the List to be filtered
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

lst.append(obj)

Matlab
No declaration or additional appending operation.

>> clear
>> 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=rand(3,4)
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

No comments yet. Be the first.

Leave a reply

Random posts

  • 放在U盘上,可以在Windows下运行的Ubuntu
  • 广受好评的鸟巢照
  • XSLT中的DISTINCT和GROUP
  • 同是底层的人
  • Cross tabulation, Pivot table and OLAP Cube summarized by tools