• Register
1 vote


For the following code :

E = [5,200];  Selected edge values 
    X = imread('LENNA128.bmp');
    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);

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.

10 7 2
6,060 points

Please log in or register to answer this question.

1 Answer

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.

11 6 4
34,950 points

Related questions

0 votes
1 answer 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?
asked Apr 28 Jack20 5.1k points
0 votes
1 answer 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 ?
asked May 1 salim1 60.6k points
0 votes
1 answer 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?
asked Mar 17 ummesalma 29.2k points
0 votes
2 answers 44 views
Problem: Each SCSI device attached to a system must be assigned an ID number. What is this ID number called?
asked Apr 6, 2020 ArifulIslam 7.5k points
0 votes
1 answer 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.
asked Oct 10, 2020 Ahsan 1.8k points
0 votes
2 answers 289 views
Problem: Array elements all have the same ____ in common? a. Pointer b. Memory location c. Value d. Data type
asked Apr 12, 2020 ArifulIslam 7.5k points
0 votes
1 answer 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?
asked Feb 19, 2020 mphil 2.3k points
0 votes
1 answer 7 views
Problem: My code is this : d = unifrnd (0,10,[10,1]); c = d.^(-2); a = round(unifrnd(0,1,[1,10]); e = a*c btotal = e+1 SIR = 1/btotal What I want is to iterate this function 1000 times, each time the value of SIR will vary due to the ... generated. For every iteration, I want the value of SIR to be added together (summed up), and in the end of the 1000th iteration, find the average SIR(mean).
asked Feb 25 Raj Tyagi 15.6k points
0 votes
1 answer 6 views
Problem: I am stuck now and need some suggestions > Matlab the expression to the left of the equals sign is not a valid target for an assignment.
asked Apr 24 ummeshani 10.8k points
0 votes
1 answer 15 views
problem: need your answer to help me?the expression to the left of the equals sign is not a valid target for an assignment Matlab
asked Mar 17 amnafahad 500 points