Matlab - Why am I getting this error - matlab

I have typed the following in matlab:
>> I=imread('23X41.jpg');
>> fun = #(x) sum(x(:).^2)/sum(x(:)).^2;
>> en= nlfilter(I,[4 4],fun);
And, got the following error?
??? Error using ==> plus
Matrix dimensions must agree.
Error in ==> nlfilter at 52
aa = mkconstarray(class(a), padval, size(a)+nhood-1);
Why am I getting this error especially that I'm sliding a 4x4 window on a 23x41 image? Why are the matrix dimensions mentioned here?
Thanks.

Make sure your image is 2D and not a color image which is a 3D array.

Related

Error using ==> mtimes MATLAB

I am trying to numerically integrate a function on MATLAB (I am on MATLAB 7.7). Here is my code.
fun = #(t)(cos(t)./sqrt(3)-sin(t)).^39*(cos(t)./sqrt(3)+sin(t)).^63*sqrt(2*cos(t).^2 + 2/3*sin(t).^2);
quad(fun,-pi/6,pi/6)
Unfortunately I am not following the error message. Can anyone help me please?
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Error in ==>
#(t)(cos(t)./sqrt(3)-sin(t)).^39*cos(t)./sqrt(3)+sin(t)).^63*sqrt(2*cos(t).^2+2/3*sin(t).^2)
Error in ==> quad at 77
y = f(x, varargin{:});
I have tried to see if the function definition is right. It seems to work for me:
fun(1)
ans =
-1.4078e-007
which the correct value I would expect when evaluated at 1. I have done several trials with various inputs, the function fun() seems to compute them alright!
p.s.: I have used quad() before this. It has worked well for me previously.
Your function works for scalar inputs, but not for vectors:
>> fun(1:5)
Error using *
Inner matrix dimensions must agree.
You need to change your function to do elementwise multiplication:
fun = #(t)(cos(t)./sqrt(3)-sin(t)).^39.*(cos(t)./sqrt(3)+sin(t)).^63.*sqrt(2*cos(t).^2 + 2/3.*sin(t).^2);

SciLab matrix assignment Invalid Index Error

I want to hide an image inside an big image using SciLab tools, following is the code snippet I am using
S1_diag = diag(s1);
S2_diag = diag(s2);
S1_diag(1:length(s1), :) = S2_diag(1:length(s1), :);
where s1 and s2 are image 1 and 2's singular diagonal matrix
The same code works in Matlab but generates an 'Invalid Index' error (21) in SciLab. What I am missing?
I am novice in SciLab syntax so couldn't understand how to address this in SciLab.
Any help is appreciated.
The reason is that the length command is not the same for Scilab and Matlab.
in Matlab, length gives the maximum dimension of a matrix. So, for a 2-by-3 matrix it is 3.
in Scilab, length gives the number of elements. So, for a 2-by-3 matrix it is 6.
Here's a little dictionary:
Matlab's length(A) is the same as Scilab's max(size(A))
Scilab's length(A) is the same as Matlab's numel(A)

Cannot plot graph in Matlab '??? Error using ==> mtimes' (Inner matrix dimensions must agree.)

when i type this equation in MatLab, I get the following error:
x=linspace(0,8*pi,1000);
y=x*sin(x);
??? Error using ==> mtimes
Inner matrix dimensions must agree.
Thank you very much!
Your question has nothing to do with plotting.
But you want element-wise multiplication. e.g., .* instead of *
x=linspace(0,8*pi,1000);
y=x.*sin(x);
http://stuff.mit.edu/afs/sipb/project/www/matlab/imatlab/node10.html

Woking with colfit in matlab

I wanted to calculate the histogram n local histogram equalisation of an input image, using colfit. But when i run the code i get the following error.
??? Error using ==> ge
Matrix dimensions must agree.
Error in ==> colfilt at 135
if all(block>=size(a)), % Process the whole matrix at once.
Error in ==> localhist at 10
z=colfilt(f,[w w],'sliding',#std);
Please provide some insights.
I haven't seen it written on the documentation (neither on help colfilt nor on docs colfilt), but I think you can only use colfilt, as nlfilter, with monochannel images. So that if you try to run the example code provided on help colfilt on a 3-channel image, say:
I = imread('peppers.png'); % 'peppers.png' is just a demo color image usually provided with matblab
figure, imshow(I)
I2 = uint8(colfilt(I,[5 5],'sliding',#mean));
figure, imshow(I2)
You get the kind of error that you posted:
Error using >=
Matrix dimensions must agree.
Error in colfilt (line 135)
if all(block>=size(a)), % Process the whole matrix at once.
If you thry this instead, which only takes the first channel (or any other combination of the channels) it will just work
% which is one of the example images usually provided with matlab
J = imread('peppers.png');
I = J(:,:,1);
figure, imshow(I)
I2 = uint8(colfilt(I,[5 5],'sliding',#mean));
figure, imshow(I2)
I hope this helps

Error in matrix index dimension for colored noise

The objective of the following code is to generate a colored gaussian signal from a random gaussian noise;perform Fast Fourier Transform and also generate power signal. Questions:
I wanted to generate gaussian colored noise such that the power spectral densities is proprtional to 1/f^beta where beta=1,-1,2,0. Is the process correct?
The code generates error
??? Error using ==> rdivide
Matrix dimensions must agree.
Error in ==> expl at 9
x = x .* 1./(f.^2);
Kindly help in solving this.Thank you
f has 1025 elements, and x has 2001 elements. But their length needs to be equal. You should change one of them.
I executed your code, then looked at the size of what you generated.
>> whos f x
Name Size Bytes Class Attributes
f 1x1025 8200 double
x 1x2001 16008 double
For the line in question to execute, you don't think that having f and x the same sizes would be useful?
When MATLAB says that matrix dimensions don't agree, it would seem the first place to look would be the dimensions of the variables in question. :)