user1769107
user1769107

Reputation: 265

How can I convert matlab code into python code?

Next code is matlab code. I want to convert this code into python code in order to use it in Arcgis.

vals = zeros(size(a(:,:,1)));
 [~,indexOfMax] = max(a,[],3);
 for i=1:size(a,1)
    for j=1:size(a,2)
        vals(i,j) = b(i,j, indexOfMax(i,j));
    end 
 end

I'll explain briefly explain this code. I have two images (named A and B). Both have 7 layers at the same dimension (4169,6289,7). First I'd like to find the location of max value in A image and then get the value of B image at the location of max value which extracted from A image in previous step.

Thanks a lot

Upvotes: 1

Views: 579

Answers (1)

nikcleju
nikcleju

Reputation: 38

vals = numpy.zeros((a.shape[0], a.shape[1]))
indexOfMax = numpy.argmax(a,2)
for i in range(a.shape[0]):
  for j in range(a.shape[1]):
    vals[i,j] = b[i,j, indexOfMax[i,j]]

It should also be possible to vectorize instead of using the for loops.

Be careful, in Python indentation is important! Keep the indentations before the second for loop and before vals.

Upvotes: 2

Related Questions