Given the following code, why are file and shadow 3D? Have we left off an outer loop over dim 3?

[rows, ~, ~] = size(file)

for irow = 1:rows

value = min(abs(file(irow,:,1) - shadow(:,:,1)).^2);

storage(irow,:) = value;

end

To take advantage of any tricks to avoid the loop, you need another dimension in your data. Note, this requires potentially excessive memory for any speedup you might gain. Assuming you want to do the same thing across the third dimension, try:

shadow = permute(shadow,[4,2,3,1]);

delta = file - shadow;

storage = min(abs(delta),[],4).^2;

If the third dimension is handled differently, you can use:

shadow_temp = permute(shadow(:,:,1),[2,3,1]);

delta = file - shadow_temp;

storage = min(abs(delta),[],3).^2;

I leave you with 2 thoughts:

- Other MVPs encourage the use of bsxfun even in R2016b+. I've never benchmarked it but I trust them when they say it is faster. I personally use implicit expansion for readability. I aim for fast enough code that is easier to read and write versus fastest possible code that coworkers may struggle with.
- Depending on your data, you may not need both the abs and .^2. Likely, the squaring accomplishes what you want the abs for, so kill the abs and suck the square inside the min call.

## 9 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606332

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606332

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606335

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606335

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606337

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606337

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606630

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606630

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606631

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606631

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606633

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606633

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606635

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606635

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606672

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606672

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606673

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/417405-remove-my-for-loop-using-meshgrid#comment_606673

Sign in to comment.