Python是否与MATLAB相当?

时间:2016-03-17 18:06:33

标签: python matlab

我正在重写一个代码

nd = unique(nd,'rows');

Mathworks的文档

  

C = unique(A,'rows')A的每一行视为单个实体,并返回A的唯一行。数组C的行按排序顺序排列。

在Python中是否有这样的东西,或者我必须使用sort?

2 个答案:

答案 0 :(得分:2)

import numpy as np

# Having an array:
a = [1,2,2,3,4,4,3]
print ("Original vector:")
print(a)

# Printting unique values of a:
print ("Unique values of a:")
print (np.unique(a))

# Other option:
print  ("Another way:")
print (set(a))

输出继电器:

Original vector:
[1, 2, 2, 3, 4, 4, 3]
Unique values of a:
[1 2 3 4]
Another way:
{1, 2, 3, 4}

答案 1 :(得分:0)

Python:

{
  "Command": "type",
  "Target": "name=username",
  "Value": "tkananthkumar"
},
{
  "Command": "type",
  "Target": "name=email",
  "Value": "Put your email here"
},
{
  "Command": "type",
  "Target": "name=password",
  "Value": "anjalai23"
},
{
  "Command": "type",
  "Target": "name=passwordConfirm",
  "Value": "anjalai23"
},
{
  "Command": "pause",
  "Target": "",
  "Value": ""
},
{
  "Command": "type",
  "Target": "name=username",
  "Value": "sowkee10"
},
{
  "Command": "type",
  "Target": "name=email",
  "Value": "Put your email here"
},
{
  "Command": "type",
  "Target": "name=password",
  "Value": "audrey12"
},
{
  "Command": "type",
  "Target": "name=passwordConfirm",
  "Value": "audrey12"
},
{
  "Command": "pause",
  "Target": "",
  "Value": ""
},
{
  "Command": "type",
  "Target": "name=username",
  "Value": "sanjuhforu"
},
{
  "Command": "type",
  "Target": "name=email",
  "Value": "Put your email here"
},
{
  "Command": "type",
  "Target": "name=password",
  "Value": "12588733"
},
{
  "Command": "type",
  "Target": "name=passwordConfirm",
  "Value": "12588733"
},
{
  "Command": "pause",
  "Target": "",
  "Value": ""
} 

这将产生与Matlab的a = np.array([[1, 0, 0], [1, 0, 0], [2, 3, 4], [2, 3, 4],[2, 3, 5],[1, 0, 0],[2,2,5],[2,3,5]]) c, ia, ic= np.unique(a,return_index=True,return_inverse=True, axis=0) 函数相同的结果。唯一的区别是在Python中从0开始的基本索引。