Search code examples
matlabfor-loop2dvectorizationnested-loops

Matlab Vectorization for nested for loops with 2D matrices


ok guys, I have edited the question.

results = zeros(96,96);
for a=1:96
    for b=1:85
        results(a,b) = abs(input(a,b) - input(a,(b+11)))
    end
end

input is also a 96x96 matrix.

Can you please help me to vectorize the loops.


Solution

  • Modified to match the edited question, and changing the name of the input matrix to avoid conflicts with matlab keywords. You end up with a 96x85 matrix occupying the first 85 rows of the zero matrix you initialized:

    results = zeros(96,96);
    results(:,1:85) = abs(inputMatrix(:,1:85) - inputMatrix(:,12:end))