フィルターのクリア

Transfer and write three lines into python: help

1 回表示 (過去 30 日間)
Mark Sc
Mark Sc 2021 年 10 月 14 日
回答済み: Yongjian Feng 2021 年 10 月 23 日
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 Center および File ExchangeCall Python from MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by