I have a loop function which the inner loop depends on the value of your outer loop.
for jj = 1:500
for ii = jj:500
Gamma(ii,jj) =mod( ii-jj, 255);
end
end
I am looking for the way to make the code is fastest: vectorized or bsxfun. Now, I am using vectorized way but it may not optimal. I ask the question to find a better solution or at least better than my way.
[iiValues, jjValues] = meshgrid(1:500, 1:500);
mask = iiValues >= jjValues; % ii >= jj
ii= iiValues(mask);
jj= jjValues(mask);
Gamma(ii,jj)=mod(ii-jj,255) % I am not sure about the line
Thanks
Using
bsxfunandtril:or using the same approach with implicit expansion (for MATLAB R2016b and later):