有时仅为分配一个索引的数组很有用.在Matlab中,这很简单:
M = zeros(4);
M(1:5:end) = 1
M =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
Run Code Online (Sandbox Code Playgroud)
Numpy有办法做到这一点吗?首先,我想要展平数组,但该操作不会保留引用,因为它会复制.我尝试使用ix_但我无法用相对简单的语法来完成它.
我试图将一个函数从类传递给其他函数参数.我收到这个错误.
错误:类型'void(A_t ::)(int)'的参数与'void(*)(int)'不匹配
有没有办法管理这个,仍然使用类a中的函数.提前致谢.
#include <iostream>
using namespace std;
void procces(void func(int x),int y);
class A_t
{
public:
A_t();
void function(int x)
{
cout << x << endl;
}
};
int main()
{
A_t a;
procces(a.function,10);
}
void procces(void func(int x),int y)
{
func(y);
return;
}
Run Code Online (Sandbox Code Playgroud) 从MATLAB帮助中考虑这个例子.
这个例子除了有语法问题外,并不适用于我.我不知道是否是版本问题,我正在使用R2013a.
classdef MyClass
properties (Constant = true)
X = pi/180;
end
properties
PropA = sin(X*MyClass.getAngle([1 0],[0 1]);
end
methods (Static = true)
function r = getAngle(vx,vy)
end
end
end
Run Code Online (Sandbox Code Playgroud)
它说
未定义的函数或变量'X'.MyClass中的错误(第1行)classdef MyClass
我可以通过添加来修复它MyClass.X,但我不知道这是否是目的.
起初我猜想k1的值不会在主空间中.但后来我意识到数组是一个指针,所以有什么区别吗?我认为这是相同的,但也许任何人都可以找到一些其他的技术差异.也许更快地传递指针?
#include <iostream>
using namespace std;
void g(double [],int );
void f(double* [],int );
int main()
{
int n = 10;
double *k1, *k2;
k1 = new double[n];
k2 = new double[n];
g(k1,n);
f(&k2,n);
for(int i = 0;i <n;i++)
{
cout << k1[i]<< " ";
cout << k2[i] << endl;
}
delete [] k1;
delete [] k2;
return 0;
}
void g(double h[],int n)
{
for(int i = 0;i <n;i++)
h[i]=i;
}
void f(double* h[],int n)
{
for(int i = …Run Code Online (Sandbox Code Playgroud)