0

I have the next code on Matlab:

cols = 7;
rows = 8;

redGrd = [0 0 0 0 0 1; 1 1 1 1 0 1; 0 0 0 0 0 1; 1 0 1 1 0 1];

redGrd(:,1)=-9999;
redGrd(1,:)=-9999;
redGrd(:,cols)=-9999;
redGrd(rows,:)=-9999

this is the result for matlab:

-9999  -9999  -9999  -9999  -9999  -9999  -9999
-9999      1      1      1      0      1  -9999
-9999      0      0      0      0      1  -9999
-9999      0      1      1      0      1  -9999
0      0      0      0      0      0      0
0      0      0      0      0      0      0
0      0      0      0      0      0      0
-9999  -9999  -9999  -9999  -9999  -9999  -9999

I want to do the same on python using numpy then I do this:

import numpy as np

cols = 7
rows = 8

redGrd = np.array([[0,0,0,0,0,1],[1,1,1,1,0,1],[0,0,0,0,0,1],[1,0,1,1,0,1]])

redGrd[:,0] = -9999
redGrd[0,:] = -9999
redGrd[:,cols] = -9999 
redGrd[rows,:] = -9999 

But the two last command dont works.

3 Answers 3

3
  1. The first line (redGrd[:,cols] = -9999) won't work, because python uses zero-based indexing, whereas Matlab starts counting from 1. So the last column in your numpy n-dimensional array has the number cols-1, or just -1 (shorthand).

A very useful resource, if you're coming from Matlab, is NumPy for Matlab Users. It lists a good number of pitfalls, such as this one.

  1. Now, the second line (redGrd[rows,:] = -9999) will fail because numpy will not attempt to increase the size of an array automatically, whereas Matlab does.

From a programmer's perspective, the former makes more sense, as it can prevent you from changing an array by accident.

If you want to "increase" the size of the array, you'll have to do it explicitly, with

    redGrd = np.vstack((redGrd, np.zeros(rows - redGrd.shape[0], redGrd.shape[1])))
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I used the comand but I got an Error message: Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: data type not understood
I forgot say Thanks so much.
1

With your initialization you are defining a 4x6 matrix:

redGrd = np.array([[0,0,0,0,0,1],[1,1,1,1,0,1],[0,0,0,0,0,1],[1,0,1,1,0,1]])

You can't access row 7 when you have only 4 rows. You should create a row x col matrix first, then fill the values as you wish. You can create the array like this:

redGrd = np.zeros((rows, cols)) 

Also, please note that in python you have the indices from 0, so you should use cols -1 instead of cols and rows -1 instead of rows

Comments

1
import numpy as np
cols, rows = 7, 8

redGrd = np.mat('0 0 0 0 0 1; 1 1 1 1 0 1; 0 0 0 0 0 1; 1 0 1 1 0 1')

# pad `redGrd[1:, 1:]` with -9999 on the top, left and right
redGrd = np.pad(redGrd[1:, 1:], ((1,0),(1,1)), mode='constant', constant_values=-9999)

# expand `redGrd` to the desired shape
redGrd.resize((rows, cols))    

# fill the last row with -9999
redGrd[rows-1, :] = -9999

print(redGrd)

yields

[[-9999 -9999 -9999 -9999 -9999 -9999 -9999]
 [-9999     1     1     1     0     1 -9999]
 [-9999     0     0     0     0     1 -9999]
 [-9999     0     1     1     0     1 -9999]
 [    0     0     0     0     0     0     0]
 [    0     0     0     0     0     0     0]
 [    0     0     0     0     0     0     0]
 [-9999 -9999 -9999 -9999 -9999 -9999 -9999]]

3 Comments

I use this command but in the np.pad I have an error message: Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'pad'
I forgot say Thanks so much.
np.pad was introduced in NumPy version 1.7.0. You could be getting that error if your NumPy version is older.

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.