Transfer and write three lines into python: help

1 visualización (últimos 30 días)
Mark Sc
Mark Sc el 14 de Oct. de 2021
Respondida: Yongjian Feng el 23 de Oct. de 2021
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 comentario
Rik
Rik el 15 de Oct. de 2021
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?

Iniciar sesión para comentar.

Respuestas (1)

Yongjian Feng
Yongjian Feng el 23 de Oct. de 2021
  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);

Categorías

Más información sobre Call Python from MATLAB en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by