Transfer and write three lines into python: help

1 vue (au cours des 30 derniers jours)
Mark Sc
Mark Sc le 14 Oct 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 commentaire
Rik
Rik le 15 Oct 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?

Connectez-vous pour commenter.

Réponses (1)

Yongjian Feng
Yongjian Feng le 23 Oct 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);

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by