gpt4 book ai didi

Matlab 中的 Python zip 函数

转载 作者:行者123 更新时间:2023-12-05 04:47:05 26 4
gpt4 key购买 nike

我有一些 Python 代码想在 Matlab 中运行。假设你有两个相同长度的列表:

    x = [0, 2, 2, 5, 8, 10]
y = [0,2, 4, 7, 3, 3]

P = np.copy(y)
P.sort()
P = np.unique(P, axis=0) # P = [0 2 3 4 7] Takes the list y, sorts it and removes repeated elements

s = list(zip(x,y)) #match list x with y: s = [(0, 0), (2, 2), (2, 4), (5, 7), (8, 3), (10, 3)]

for y_1,y_2 in zip(P,P[1:]): # runs over (0, 2), (2, 3), (3, 4), (4, 7)
for v_1,v_2 in zip(s, s[1:]):
-- process --

在这种情况下:

list(zip(s, s[1:])) = [((0, 0), (2, 2)), ((2, 2), (2, 4)), ((2, 4), (5, 7)), ((5, 7), (8, 3)), ((8, 3), (10, 3))]

我想在 Matlab 中翻译它,但我不知道如何复制 zip 函数。关于如何执行此操作的任何想法?

最佳答案

MATLAB 没有zip,但您可以通过多种方式完成同样的事情。我认为最简单的翻译


for y_1,y_2 in zip(P,P[1:]):
...

for ii = 1:numel(P)-1
y_1 = P(ii);
y_2 = P(ii+1);
...

一般来说,迭代 zip(x,y) 是通过迭代索引 1:numel(x) 来完成的,然后使用循环索引索引到数组 xy

关于Matlab 中的 Python zip 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68604380/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com