# Matrix multiplication within a function not working

2 views (last 30 days)
Bharath Nagarajan on 19 Sep 2021
Commented: Bharath Nagarajan on 19 Sep 2021
The following function creates a column vector and an image histogram:
function lutlum = lutlummer(image)
for i = 1:1:265
lutlum(i, 1) = i-1;
end
%multiplying the LUT with the luminescence histogram of the image
%Finding luminance image 'L' (Formula gained from class slides)
L = uint8(sum(bsxfun(@times,double(image),reshape([0.299 0.587 0.114],[1 1 3])),3));
%Finding luminance histogram'HL'
HL = histcounts(L,[0:256])';
lutlum = lutlum*HL;
end
The function is called with an image such that HL is a 1x2 matrix, so multiplying a 265x1 matrix ('lutlum') with a 1x2 matrix should be no problem. however, i still get this message:
Matrix dimensions must agree.
lutlum = lutlum.*HL;
Please do let me know where i am going wrong
Bharath Nagarajan on 19 Sep 2021
L is supposed to be the 'Luminescence image' and it appears that it is a greyscale image. Howvere, I am trying to multiply lutlum (256x1 matrix) with HL (a 1x2 matrix). this should still work though right? since the matrix dimentions are compatable?

Sulaymon Eshkabilov on 19 Sep 2021
There is an err, here is how it is to be:
lutlum = HL.'*lutlum; % creates 2 - by - 256
Bharath Nagarajan on 19 Sep 2021
Unfortunately it still throws the same error
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
lutlum = lutlum.'*HL;
and even when the order of matrices are switched:
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
lutlum = HL.'*lutlum;

Sulaymon Eshkabilov on 19 Sep 2021
There are a couple of errs in the code. Here is the corrected version:
function lutlum = lutlummer(image)
for i = 1:1:256 % Has to be 256 NOT 265. 256 similar to 265 :)
lutlum(i, 1) = i-1;
end
%multiplying the LUT with the luminescence histogram of the image
%Finding luminance image 'L' (Formula gained from class slides)
L = uint8(sum(bsxfun(@times,double(image),reshape([0.299 0.587 0.114],[1 1 3])),3));
%Finding luminance histogram'HL'
HL = histcounts(L,[0:256])'; % Note the size of HL is 256 - by - 1 NOT [1 - by - 2] as you have stated
lutlum = lutlum*HL'; % Transpose is necessary
end

R2020a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!