Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.6:旋转矩阵_Python_Python 3.x - Fatal编程技术网

Python 3.6:旋转矩阵

Python 3.6:旋转矩阵,python,python-3.x,Python,Python 3.x,我有强度数据(SEM.txt),我想通过将行重新指定给列来将图像旋转90度。Python给了我一个“代码分析无效语法”错误,它说“for m:”-我做错了什么 import numpy as np import matplotlib.pyplot as plt a=np.loadtxt("SEM.txt") Intensity=np.loadtxt("SEM.txt") Intensity[n,m]=Raw_Intensity for m: for n: New_Int

我有强度数据(SEM.txt),我想通过将行重新指定给列来将图像旋转90度。Python给了我一个“代码分析无效语法”错误,它说“for m:”-我做错了什么

import numpy as np
import matplotlib.pyplot as plt

a=np.loadtxt("SEM.txt")
Intensity=np.loadtxt("SEM.txt")
Intensity[n,m]=Raw_Intensity
for m:
    for n:
        New_Intensity[m,n]=Raw_Intensity[n,m]
plt.imshow(New_Intensity)

您的for循环语法不正确。你需要像这样的东西

for i in some_iterable:
    #do some stuff with i or whatever
虽然我很确定你可以用这样的列表替换这些循环

New_Intensity=[[x[1],x[0]] for x in Raw_Intensity]
假设这就是你想要做的

编辑

根据您上面关于需要使用for循环的评论,您可以这样做

New_Intensity=[]
for x in Raw_Intensity:
    #we don't need to define x outside the for loop
    New_Intensity.append([x[1],x[0]])

您正在做的是转置数组。直接使用转置。看

下面是我建议的演示

import numpy as np
a = np.array([[1,2,3],[4,5,6]])
print(a)
[[1 2 3]
 [4 5 6]]
b = np.transpose(a)
print(b)
[[1 4]
 [2 5]
 [3 6]]
如果您想手动执行此操作,则可以采用这种方式

# get the size of the matrix
size = a.shape()
# create the output of the correct size
c = np.zeros((size[1],size[0]))
# iterate over the range of row values
for m in range(size[0]):
# iterate over the range of column values
 for n in range(size[1]):
  c[n,m]=a[m,n]
print(c)
[[ 1.  4.]
 [ 2.  5.]
 [ 3.  6.]]

结果与仅使用
numpy.transpose
相同,但需要更多的输入。

您会得到什么错误?由于我们没有您的数据,如果没有错误代码,我们无法帮助您。代码分析无效语法“I做错了吗?”嗯,主要是for循环语法。此外,没有定义m和n。你可以把图像换过来,这条线应该做什么
Intensity[n,m]=原始强度
此外,如果您只需要转置,可以执行
Intensity.T
# get the size of the matrix
size = a.shape()
# create the output of the correct size
c = np.zeros((size[1],size[0]))
# iterate over the range of row values
for m in range(size[0]):
# iterate over the range of column values
 for n in range(size[1]):
  c[n,m]=a[m,n]
print(c)
[[ 1.  4.]
 [ 2.  5.]
 [ 3.  6.]]