• 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 80 views
Problem : Looking for solution on following issue error: the expression to the left of the equals sign is not a valid target for an assignment
asked Nov 15, 2019 peterlaw 6.9k points
0 votes
1 answer 182 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 42 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 2 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 2 days ago Ashty 1.1k points
0 votes
2 answers 181 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 244 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 280 views
Problem : I have very simple problem that currently looking for the fast implementation in Matlab. I have below array of values: b = floor(rand(5,5).*255) I also have the similarly sized threshold array : b_thresh = floor(rand(5,5).*255) I want to use the ... But, I am facing below error : Operands to || and && operations must be convertible to logical scalar values How can I resolve the issue?
asked Dec 21, 2019 alecxe 7.5k points
0 votes
1 answer 61 views
Problem: Which of the following is NOT a rule that must be followed when naming identifiers? A. Identifiers for constants should use an underscore between words. B. Identifiers should use periods to separate words. C. Identifiers should list the data type at the end of the name. D. Identifiers can contain spaces.
asked Mar 9, 2020 ArifulIslam 7.5k points
0 votes
1 answer 16 views
Problem: I want to find the sum of all the ellements in a matrix. It is for a project. Please can someone give me pointers for that.
asked Oct 22, 2020 Ahsan 1.8k points
0 votes
1 answer 19 views
My problem to solve is this: Make a structure called student, in which you will have the fields: name, age, average and data will be requested from the user of how many students will enter and check which of them has the best average and print it, use pointers. I would ... struct Student {      char name [30];      int age;      average float; } student [nStudents], * studentPointer = student;
asked Aug 14, 2020 game 4.6k points