Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python zip function in Matlab

I have some Python code that I would like to run in Matlab. Suppose you have two lists of the same length:

    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 --

Where in this case:

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))]

I would like to translate this in Matlab but I don't know how to replicate the zip function. Any ideas on how I can do this?

like image 843
NoetherNerd Avatar asked Sep 02 '25 16:09

NoetherNerd


2 Answers

MATLAB doesn’t have a zip, but you can accomplish the same thing in various ways. I think that the simplest translation of


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

is

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

And in general, iterating over zip(x,y) is accomplished by iterating over indices 1:numel(x), then using the loop index to index into the arrays x and y.

like image 126
Cris Luengo Avatar answered Sep 05 '25 09:09

Cris Luengo


Here's an implementation of Python's zip function in Matlab.

function out = zip(varargin)
% Emulate Python's zip() function.
%
% Don't actually use this! It will be slow. Matlab code works differently.
args = varargin;
nArgs = numel(args);
n = numel(args{1});
out = cell(1, n);
for i = 1:n
    blah = cell(1, nArgs);
    for j = 1:nArgs
        if iscell(args{j})
            blah(j) = args{j}(i);
        else
            blah{j} = args{j}(i);
        end
    end
    out{i} = blah;
end
end

But don't use it; the performance will be lousy. In Matlab, you want to keep things in parallel primitive arrays instead, and use vectorized operations on those. Or, if you have to, iterate over array indexes.

like image 21
Andrew Janke Avatar answered Sep 05 '25 10:09

Andrew Janke