Matlab error - Matrix dimensions must agree - matlab

cc=imread('<a href=“http://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Pavlovsk_Railing_of_bridge_Yellow_palace_Winter.jpg/250px-Pavlovsk_Railing_of_bridge_Yellow_palace_Winter.jpg”>wintersm.jpg</a>');
c=rgb2gray(cc);
x=ones(256,1)*[1:256];
c2=double(c).*(x/2+50)+(1-double(c)).*x/2;
c3=uint8(255*mat2gray(c2));
t=graythresh(c3);
ct=im2bw(c3,t);
This is a code i have written to threshold the image but cant execute because of the error " ==> times
Matrix dimensions must agree. " . I am new to matlab and i cant figure out how to solve this problem. Please Help.

I looked at the image, it is of size 169x250 . Hence size(c) = [169 250] while size(x) = [256 256]. Since .* operation between c and x needs both of them to be of same size , hence it is giving the error.
Redefine x so that its size matches the size of c

I ran your code but added the following line before c2=double(c).*(x/2+50)+(1-double(c)).*x/2;, I added this:
size(x)
size(c)
and you get the following print out:
ans =
256 256
ans =
169 250
Which is essentially saying, the image isnt the size you think it is and you are mixing your matrix sizes.

Related

Subscripted assignment dimension mismatch in MCMV code

The below code gives me a error:
Subscripted assignment dimension mismatch.
Error in ==> lookmcvmt at 18
M(:,:,j,i) = mcmvOUT2((k+1):(k+Nz), i:Nt:Nr);
Please help to solve.
load MCMVout1xzy
mcmvOUT2 = MCMVout1xzy;
whos
[Nr2 Nr] = size(mcmvOUT2);
Ny = 51;
Nx = 51;
Nz = 41;
Nt = 10;
M = zeros(Nz,Nx,Ny,Nt);
for j=1:Ny
for i=1:Nt
k = Nz*(j-1);
M(:,:,j,i) = mcmvOUT2((k+1):(k+Nz), i:Nt:Nr);
end
end
The error 'subscripted assignment dimension mismatch' means you are trying to assign a block of values into a space that is the wrong size.
This entity
mcmvOUT2((k+1):(k+Nz), i:Nt:Nr);
represents a matrix of values in 2 dimensions. Its size is defined by the two ranges specified by (k+1):(k+Nz) and i:Nt:Nr - you can check its size by typing
size(mcmvOUT2((k+1):(k+Nz), i:Nt:Nr))
The space you are trying to fit it into has to be exactly the same dimensions. The size of the range specified by
M(:,:,j,i)
is defined by the Nz and Nx arguments to the zeros call with which you preallocated the array.
We can't test this because the MCMVout1xzy file containing your data is not given, but you will be able to solve this yourself by using the size command and making sure all your dimensions match.
Because matlab uses column-wise indexing, and a lot of us are used to the row-wise paradigm of cartesian coordinate systems, getting the order of your indexes right can be confusing - this is the root of a lot of these kinds of error (for me anyway).
Things to check: your dimensions Nz etc. are correct and the order of your Nz etc. variables in the zeros call is correct.

MAP must be at least 'm x 3 array' error in MATLAB

I am trying to generate C code with the MATLAB Coder. The input to the function is an image that has been processed by imread within MATLAB. Since the output should be an m x n x 3 array from imread, I am not sure why this error is being shown. The assertions at the start of the function are shown below. Following that is the rgb2gray which is the source of the error.
%#codegen
assert(isa(IM, 'uint8'));
assert(size(IM, 1) < 100);
assert(size(IM, 2) < 100);
assert(size(IM, 3) == 3);
I_temp = rgb2gray(IM);
The error report I seem to be getting is shown below:
The outputs and inputs to the function are given below:
function [actual_lep_x, actual_lep_y, actual_rep_x, actual_rep_y, actual_lmp_x, actual_lmp_y, actual_rmp_x, actual_rmp_y, actual_lnp_x, actual_lnp_y, np_x, np_y] = Points( IM )
I can send over the .m file if anyone needs to compile it.
Help will be much appreciated!
If your code is exactly same as what you have shown in the image, then the error is obvious. IM seems to a filename, since on the line above, you have done I_ttemp=imread(IM) (which is commented for an unknown reason). Now, since rgb2gray did not receive an m x n x 3 array, other argument it excepts is a colormap which has dimensions m x 3. However, you give a filename, which is of the form 1 x m. Thus the error.
You should write:
I_temp=imread(IM);
if size(I_temp,3)==3
I_temp=rgb2gray(I_temp);
end
%do processing on I_temp.

For loop in matlab error "Attempted to access index out of bounds because..."

I'm new to matlab.
I wrote a function.
when i run my function in workspace at the first FOR i get this error :
??? Attempted to access XX(461,:); index out of bounds because
size(XX)=[460,440,3].
Error in ==> FOH_Zoom at 10
XX(j,:)=(XX(2*j-1,:)+XX(2*j+1,:))/2;
and this is my function :
function XX = FOH_Zoom(img)
X = ones(size(img)); %make a matrix of ones as size as image
XX=imresize(X,2); %make size of matrx double
[a,b]=size(X); %get the size of matrix
XX(1:2:end,1:2:end)=img(:,:); % fill odd rows and columns with original image data
[m,n]=size(XX);
for j=1:m-1
XX(j,:)=(XX(2*j-1,:)+XX(2*j+1,:))/2;
end
for i=1:n
XX(:,i)=(XX(:,2*i-1)+XX(:,2*i+1))/2;
end
imshow(XX); % show image
title([num2str(m),' *** ',num2str(n)]);
end
what is the problem ,do you think?
any help really appriciate
regards.
Your XX is 3D, so you have to use size like this,
[m,n,~]=size(XX);
Because:
XX=zeros(460,440,3);
[m,n]=size(XX)
m = 460
n = 1320
While,
[m,n,~]=size(XX)
m = 460
n = 440
Second Problem
in Line 10 you have,
XX(j,:)=(XX(2*j-1,:)+XX(2*j+1,:))/2;
When m = 231 then 2xm-1 = 461, and you will get the error out of bound,
I don't know what you want to do, but changing for j = 1 : m/2 and for i = 1 : n/2 would help.

Quadtree decomposition

I tried the quadtree decomposition using the following code, but every time I am getting an error.
>> I=imread('hyd.tif');
>> S=qtdecomp(I)
Or
>> I=imread('hyd.tif');
>> S=qtdecomp(I,.27)
Error:
??? Error using ==> qtdecomp>ParseInputs at 145
A must be two-dimensional
Error in ==> qtdecomp at 88
[A, func, params, minDim, maxDim] = ParseInputs(varargin{:});
The culprit is due to your image being in colour or RGB. Try converting the image to grayscale before using the algorithm.
I = imread('hyd.tif');
Igray = rgb2gray(I);
S = qtdecomp(Igray);
Also make sure that your image dimensions are of a power of 2 or else the quadtree decomposition algorithm will not work.

can't add two matricies of the same size together

I'm trying to add two matrices together. They are both 400x400. Here's the bit of code that's giving me trouble:
greys = (r+g+b)./3;
fc = cat(3, r, g, b);
combined = (greys+fc)./2; <---error occurs here
But when my code gets to the greys+fc part, it throws an error. This error:
Error using +
Matrix dimensions must agree.
Error in imgSimpleFilter (line 36)
combined = (greys+fc)./2;
when I print the number of rows and columns in both the grey and fc matricies, I get 400 for all of the values (which is exactly as I expected, as I am working with a 400x400 image).
Why isn't it letting me add these together?
I have no problems with the line
greys = (r+g+b)./3;
and that's adding three 400x400 matrices together. Any ideas?
You can't add them because greys is 400x400, while fc is 400x400x3.
Try typing size(greys) and size(fc) on the command line, or whos greys fc to see it.
If you want to "combine" them by averaging them, you could use bsxfun:
combined = bsxfun(#plus, greys, fc) ./ 2;