Transfer and write three lines into python: help
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
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?
Respuestas (1)
Yongjian Feng
el 23 de Oct. de 2021
- The numpy.sum doesn't take 'all'
- 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);
0 comentarios
Ver también
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!