# In an assignment a(:) = b, the number of elements in a and b must be the same

1 vote
74 views

## Problem:

For the following code :

``````E = [5,200];  Selected edge values
N = length(X);
Y = false(N+2);
for k = 1:numel(E);
Y(2:end-1,2:end-1) = X==E(k);
Z = Y(1:end-2,2:end-1) | Y(3:end,2:end-1) | Y(2:end-1,3:end) | Y(2:end-1,1:end-2);
X(Z) = round((X(end-3,3:end-2) + X(end-3,4:end-1))/2);
end``````

I get the error message:

``````In an assignment  A(:) = B, the number of elements in A and B must be the same
``````

Can anyone help me out to solve this problem.

1 vote

The problem with you code is explained below:

``````Problem 1: The use of length with a 2D matrix
If your image is not square, N will be the size of the largest dimension of your image. Y will always be a square image two pixels larger in both dimensions than the largest dimension of your image. The assignment
Y(2:end-1,2:end-1) = X==E(k)
will then fail as you're trying to assign a non-square matrix to a square matrix.
The simplest way to fix this is to declare:
N = size(X); %%assuming that |X| is a greyscale or indexed image.
Problem 2: Most likely the one that causes the error message:
X(Z) = round((X(end-3,3:end-2) + X(end-3,4:end-1))/2)

Z is a logical array which will select as many element of X as there are true values in Z. The other side of the assignment is the moving average of one the last row of the image excluding the first two pixels. It is a row vector with 2 less elements than the number of columns of X. I don't see why this would have as many values as there are true values in Z.``````

Hope you understand the problems and can solve it now.

Happy coding.

## Related questions

10 views
Problem: So far, I don't get any ideas to understand what this error means. If I have, I think I could solve this problem in my Matlab programming. If anyone could help me to explain what's going on and give some examples?
4 views
Problem: I want to convert a 2x2 cell into a simple 2x2 matrix in the code shown below. In doing so, I am facing the error The error is due to the line "K= reshape(cat(1,P{:}),size(P)) " What is wrong here ?
16 views
Problem: With the following code, I get the error message To RESHAPE the number of elements must not change. Can anyone help me?
44 views
Problem: Each SCSI device attached to a system must be assigned an ID number. What is this ID number called?
318 views
Problem: I am trying to plot two vectors in MATLAB and it keeps giving me the error that error in plot vector must be same length. What to do? Some one please help.
289 views
Problem: Array elements all have the same ____ in common? a. Pointer b. Memory location c. Value d. Data type
349 views
Problem : I am new to matlab and nntool. Currently I want to implement the neural network with the use of matlab using nntool. I am facing below error message. I am unable to understand the error message. **??? Insufficient number of outputs from right hand ... : Insufficient number of outputs from right hand side of equal sign to satisfy assignment.** How can I fix the above error message?