How do I fix the error: In an assignment A(:) = B, the number of elements in A and B must be the same.?
Show older comments
Hello everyone,
I am trying to assign an RGB value to a line drawn from input coordinates in an inputted colour image. When I use my function from the command window to execute the task I get an error. Below is my function code and the line where I get my error. May someone kindly help me fix the error.
Error in Linecol (line 35) imgColour(indices) = [cR cG cB]; %Assigning a colour to the drawn line.
function [imgColour] = Linecol(imgIn, P1, P2, RGB)
%*/LINE PLOTER BETWEEN TWO INPUT COORDINATES IN A COLOUR INPUT IMAGE/*
By Allycan
Part 2
DATE: 04 September 2015
Version A
%}
%______________________________________________________________________
%---------------COORDINATE AND COLOUR REFERENCING----------------------
P1r = P1(1);
P1c = P1(2);
P2r = P2(1);
P2c = P2(2);
cR=RGB(1);
cG=RGB(2);
cB=RGB(3);
%--------------------------INPUT IMAGE---------------------------------
imgColour = imgIn;
%------------------------DRAWING THE LINE------------------------------
rowpoints = linspace(P1r,P2r,2000); %Getting the row values/points that will make up the line.
columnpoints = linspace(P1c,P2c,2000); %Getting the column values/points that will make up the line.
indices = sub2ind(size(imgColour),round(rowpoints),round(columnpoints)); %Converting the row and column values/points to linear indices/(Creation of the line).
imgColour(indices) = [cR cG cB]; %Assigning a colour to the drawn line.
end
Accepted Answer
More Answers (1)
You have defined each of rowpoints and columnpoints to have 2000 elements. These values are then converted to linear indices using 2ub2ind, which means the variable indices also has 2000 elements.
The values of cR, cG and cB are all scalars, being defined by cR=RGB(1), cG=RGB(2), and cB=RGB(3). These are then combined into a 1*3 vector using [cR cG cB].
So the line
imgColour(indices) = [cR cG cB];
is trying to fit a vector of three elements into two thousand elements, thus the error. It is not really clear what you intend to achieve with this code, but remember that when allocating values using indexing either:
- the number of elements on the LHS and RHS must be the same (e.g. three into three), or
- the RHS can be scalar.
Probably the easiest solution would be for you to use the second of these and treat each of the R, G, and B separately (as scalars).
1 Comment
Ally Mapunda
on 4 Sep 2015
Edited: Ally Mapunda
on 4 Sep 2015
Categories
Find more on Convert Image Type in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!