Python到Matlab的转换?
下面是我的python代码(用于气泡排序)。下面是我将其转换为MATLAB代码的尝试。我是MATLAB新手,我正在做转换练习。如果您能提供有关我的转换是否准确/不正确的反馈,我将不胜感激 python版本:Python到Matlab的转换?,python,matlab,Python,Matlab,下面是我的python代码(用于气泡排序)。下面是我将其转换为MATLAB代码的尝试。我是MATLAB新手,我正在做转换练习。如果您能提供有关我的转换是否准确/不正确的反馈,我将不胜感激 python版本: def bubble_sort(alist): return bubble_sort_helper(alist, len(alist)) def bubble_sort_helper(alist, n): if n < 2: return alist
def bubble_sort(alist):
return bubble_sort_helper(alist, len(alist))
def bubble_sort_helper(alist, n):
if n < 2:
return alist
for i in range(len(alist)-1):
if alist[i] > alist[i+1]:
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
return bubble_sort_helper(alist, n-1)
def bubble_排序(列表):
返回气泡\排序\辅助对象(列表,len(列表))
def bubble_sort_helper(列表,n):
如果n<2:
返回列表
对于范围内的i(len(alist)-1):
如果alist[i]>alist[i+1]:
temp=alist[i]
alist[i]=alist[i+1]
alist[i+1]=温度
返回气泡\排序\辅助对象(列表,n-1)
我的MATLAB转换尝试:
function a = bubble_sort(alist)
a = bubble_sort_helper(alist, size(alist))
end
function b = bubble_sort_helper(alist, n)
if n < 2
b = alist
end
for ii = size(alist)
if alist(1) > alist (ii+1)
temp = alist(ii)
alist(ii) = alist(ii+1)
alist(ii+1) = temp
end
end
b = bubble_sort_helper(alistn n-1)
end
函数a=bubble\u排序(列表)
a=气泡\排序\辅助对象(列表,大小(列表))
结束
函数b=气泡\u排序\u辅助对象(列表,n)
如果n<2
b=阿利斯特
结束
对于ii=尺寸(列表)
如果列表(1)>列表(ii+1)
温度=1(二)
alist(ii)=alist(ii+1)
列表(ii+1)=温度
结束
结束
b=气泡排序辅助程序(alistn n-1)
结束
这里有几个问题:
numel
而不是size
来获取数组中的元素数size
将为您提供每个维度的大小向量,numel
将为您提供元素总数for
循环创建一个值数组。为此,请使用冒号创建一个从2
到n
的数组
for ii = 2:n
end
ii
作为循环变量,但尝试在循环内部使用i
。选择一个并坚持(最好不要i
)alist([i-1, i]) = alist([i, i-1]);
function a = bubble_sort(alist)
a = bubble_sort_helper(alist, numel(alist))
end
function b = bubble_sort_helper(alist, n)
if n < 2
b = alist;
else
for k = 2:n
if alist(k-1) > alist(k)
alist([k-1, k]) = alist([k, k-1]);
end
end
b = bubble_sort_helper(alist, n-1);
end
end
alist([i-1, i]) = alist([i, i-1]);
function a = bubble_sort(alist)
a = bubble_sort_helper(alist, numel(alist))
end
function b = bubble_sort_helper(alist, n)
if n < 2
b = alist;
else
for k = 2:n
if alist(k-1) > alist(k)
alist([k-1, k]) = alist([k, k-1]);
end
end
b = bubble_sort_helper(alist, n-1);
end
end
函数a=bubble\u排序(列表)
a=气泡\排序\辅助对象(列表,numel(列表))
结束
函数b=气泡\u排序\u辅助对象(列表,n)
如果n<2
b=列表;
其他的
对于k=2:n
如果alist(k-1)>alist(k)
alist([k-1,k])=alist([k,k-1]);
结束
结束
b=气泡排序辅助程序(列表,n-1);
结束
结束
您的python版本效率低下:
def bubble_sort(alist):
return bubble_sort_helper(alist, len(alist))
def bubble_sort_helper(alist, n):
if n < 2:
return alist
for i in range(n-1):
if alist[i] > alist[i+1]:
alist[i], alist[i+1] = alist[i+1], alist[i]
return bubble_sort_helper(alist, n-1)
def bubble_排序(列表):
返回气泡\排序\辅助对象(列表,len(列表))
def bubble_sort_helper(列表,n):
如果n<2:
返回列表
对于范围(n-1)内的i:
如果alist[i]>alist[i+1]:
alist[i],alist[i+1]=alist[i+1],alist[i]
返回气泡\排序\辅助对象(列表,n-1)
您的matlab代码是错误的:
function a = bubble_sort(alist)
a = bubble_sort_helper(alist, size(alist))
end
function b = bubble_sort_helper(alist, n)
if n < 2
b = alist;
else
for i = 2:n
if alist(i-1) > alist(i)
temp = alist(i-1);
alist(i-1) = alist(i);
alist(i) = temp;
end
end
b = bubble_sort_helper(alist, n-1);
end
end
函数a=bubble\u排序(列表)
a=气泡\排序\辅助对象(列表,大小(列表))
结束
函数b=气泡\u排序\u辅助对象(列表,n)
如果n<2
b=列表;
其他的
对于i=2:n
如果alist(i-1)>alist(i)
温度=1(i-1);
alist(i-1)=alist(i);
alist(i)=温度;
结束
结束
b=气泡排序辅助程序(列表,n-1);
结束
结束
我忘了在python代码的if语句下添加缩进。通过编辑修复了这个问题,我的错误。您是否测试过它是否对您的输入进行了排序?1)它是否按预期工作?2) 为什么它是递归的?请注意,在您的python代码中,您可以执行alist[i],alist[i+1]=alist[i+1],alist[i]
@Suever,我运行了python代码,它工作正常,但上面发布的原始MATLAB代码没有运行。哦,感谢您提供的关于我效率低下的提示。我正在尽我所能学习排序算法,所以每一点都有帮助。