2

I am actually translating a matlab script into python an i have a problem using arrays in python (I am still a beginner) numpy. My question is this: In matlab I am computing the fourier transform of several signals and I am storing dynamically it in a 3 by 3 array say U. A simple example of what i want to do is as follows;

l = 3 ;
c = 0 ;
for i = 1:3
    for j = 1:10
        c=c+1 ;
        a = j + 1;
        U(i,c,:)=a ;
    end 
end

I want to translate this to python and I am unable to create the array U that stores dynamically the value of 'a' in U. Note : Here am computing 'a' as j+1 for simplicity but in my script 'a' is an array (the fourier transform of a signal)

Sorry for my bad english, I am french. T

4
  • 1
    What is the python code that you've tried? What errors are you getting? Assignment works very similarly in numpy. Commented Feb 9, 2017 at 17:55
  • MATLAB grows an matrix as you assign to it. numpy requires that you initialize an array of the correct size first. See this recent question, also titled dynamically, stackoverflow.com/questions/42041016/… Commented Feb 9, 2017 at 18:26
  • 1
    It seems like you know the size of the array beforehand. So you can first pre-allocate the array as: mat = np.empty(shape=(3, 30)). Check if I got the shape right. Commented Feb 9, 2017 at 18:27
  • @hpaulj Technically MATLAB doesn't grow the array, it creates a new array and copies all the data over. It is extremely slow and you shouldn't be doing it in this situation. You can do the same thing in numpy if you really wanted to. The difference is that numpy makes you do the copy explicitly so people aren't fooled into thinking the array is somehow dynamically size. Commented Feb 10, 2017 at 22:43

1 Answer 1

0

I believe you will ultimately want something like this. One of the things that was confusing was what your loop variable c and j were doing. It seems like you wanted c=j, so I changed that below. The one thing you need to watch out for is that python objects are indexed from 0, whereas Matlab objects are index from 1. So below, if you actually start examining the values of i and j, you will see that they start from 0.

import numpy
L = 3;
C = 10;
N = 50; # Size of the Fourier array
U = numpy.zeros((L,C,N))
for i in range(L):
    for j in range(C):
        # Create a matrix of scalars, for testing
        a           = i*j*numpy.ones((N,));
        U[i,j,:]    = a;
Sign up to request clarification or add additional context in comments.

14 Comments

Thank's very much! it's exactly what I wanted
I have question on the dimensions of U, normally it's has dimension 3 but when I do np.shape(U[1,2,:]) U seems to have a dimension of 2
Right because you are pulling off a submarine in that case. Please mark the answer correct if it is.
I still don't understand why the dimension is not 3
Because you are extract a matrix the the same size as 'a', and 'a' has only 2 dimesions. You can think of the 3rd dimension being implicitly equal to 1.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.