简体   繁体   中英

Matlab - Changing specific elements that meet condition

I'm not experienced in matlab so I'm trying to write efficient code

I have map500 array and I want to modify the array's specific location stated below with the specific condition.

map500(map500(x1:x2,y1:y2) < prob(1)) = prob(1);

Here I want to change values between rows x1 and x2, columns y1 and y2 with the value in prob(1) if and only if the value in subarray is smaller than prob(1) but this code does not work and if I run 2 for loop, it consumes too much time since I have to run so many loops.

You can use the max function to replace the values that are less than prob(1). To focus exclusively on the subarray, the following should work:

map500(x1:x2,y1:y2) = max(prob(1),map500(x1:x2,y1:y2));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM