简体   繁体   English

ValueError:矩阵未针对复制错误和x [:]对齐

[英]ValueError: matrices are not aligned for copy error and x[:]

I got the ValueError as given below. 我得到了ValueError如下。

ValueError: matrices are not aligned for copy error ValueError:矩阵未针对复制错误进行对齐

It was traced to the following line (I did not write this code, I am trying to use it): 它被追溯到以下行(我没有编写此代码,我正在尝试使用它):

x1[:] =  _dotproduct(x1, u)

The dot product is like numpy dot product, it works FINE, printing _dotproduct(x1, u) give a valid answer. 点积类似于numpy点积,它可以很好地工作, printing _dotproduct(x1, u)给出有效答案。 It is x1[:] that is not working. x1[:]不起作用。

What does [:] mean? [:]是什么意思? I have never seen that. 我从未见过。

Also how can I solve the error of alignment? 另外,如何解决对齐错误?

Edit: 编辑:
I have now traced the error to x1[:] , so instead of this can I do the following: 现在,我已将错误跟踪到x1[:] ,因此可以代替以下操作:

hh=len(x1)

x1[0:hh]=_dotproduct(x1, u) ? x1[0:hh]=_dotproduct(x1, u)吗?

In this case, since it's on the left side of the = sign, it's a slice assignment. 在这种情况下,由于它位于=符号的左侧,因此是切片分配。 The object x1 remains the same object, but all its contents are replaced with the sequence on the right. 对象x1仍然是同一对象,但是其所有内容都被右侧的序列替换。 Without the [:] , x1 would be assigned to a totally different object. 如果没有[:] ,则x1将分配给完全不同的对象。

Using a slice assignment means that if there are other references to the same variable in your program, all of these will see the new contents. 使用分片分配意味着,如果程序中存在对同一变量的其他引用,则所有这些引用都会看到新内容。 For example, the caller of a function passes in a container and the function replaces its contents. 例如,函数的调用者传入一个容器,该函数替换其内容。 This wouldn't be possible without the slice assignment. 没有切片分配,这将是不可能的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM