Transfer and write three lines into python: help

1 次查看(过去 30 天)
Hi all,
Please anyone help me in writing the following lines in python:
clear all;
clc;
x=[5,31,41,51,61]
y=[1,11,21,31,5;4,14,24,34,5;
7,17,27,37,5;34,44,54,64,5;37,47,57,67,5]
for i length(x):-1:1
if (sum(y==x(i),'all')<.1)
x=x-(x>y(i))
end
end
in python:
import numpy as np
x=np.array([5,31,41,51,61])
y=np.array([[1,11,21,31,5],[4,14,24,34,5],
[7,17,27,37,5],[34,44,54,64,5],[37,47,57,67,5]])
for i in range(len(x)-1,2,-2):
if (np.sum(y==x[i],'all')<.1):
x=x-(x>y[i])
  1 个评论
Rik
Rik 2021-10-15
This is a Matlab forum, so this isn't the right place to ask for help with python.
I don't see why the code you wrote wouldn't do what you expect. The only thing I notice is that you have the number 2 as argument to the range function, while in Matlab your step size is 1. Are you sure that is correct?

请先登录,再进行评论。

回答(1 个)

Yongjian Feng
Yongjian Feng 2021-10-23
  1. The numpy.sum doesn't take 'all'
  2. The for loop needs to be adjusted to 0-base
import numpy as np
x=np.array([5,31,41,51,61])
y=np.array([[1,11,21,31,5],[4,14,24,34,5],
[7,17,27,37,5],[34,44,54,64,5],[37,47,57,67,5]])
for i in range(len(x)-1,0,-1):
if (np.sum(y==x[i])<.1):
x=x-(x>y[i])
print(x);

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by