How can I fix error matrix dimensions must agree?
Show older comments
This is suppose to produce any shape on a graph and shift it 4 units to the right and 2 units down with one input. I am getting told that there is error in matrix dimensions.
function [Row1,Row2] = problem3(nSides)
r = 1;
theta = pi./nSides .* (1:2:2*nSides-1);
Row1 = r .* cos(theta);
Row2 = r .* sin(theta);
fill(Row1+4,Row2-2,'k');
axis square; grid on;
end
10 Comments
Geoff Hayes
on 19 Apr 2014
Hi Gimil,
At what line are you getting the error message, and what exactly is it? What is your input nSides - a scalar or vector or matrix?
Geoff
Azzi Abdelmalek
on 19 Apr 2014
What is the size of nSides?
Sara
on 19 Apr 2014
You should report the input value you are using and paste here the exact error you get from matlab to get help
Gimil
on 19 Apr 2014
Gimil
on 19 Apr 2014
Gimil
on 19 Apr 2014
Geoff Hayes
on 19 Apr 2014
There's the problem - the input is formatted with an x coordinate and a y coordinate i.e. [1;2] for coordinate (1,2) and so is not a scalar. This will raise the error when theta is calculated as pi./nSides is a 2x1 vector and (1:2:2*nSides-1) is a 1x4 vector. The calculation for theta will have to be clarified given that the input is a coordinate...
Gimil
on 19 Apr 2014
Image Analyst
on 19 Apr 2014
Edited: Image Analyst
on 19 Apr 2014
My answer below works just fine. Try this:
[Row1,Row2] = problem3(5);
It produces two plots. The original one, and the shifted one. Isn't that what you want?
Geoff Hayes
on 19 Apr 2014
Re-reading the problem definition a second time indicates that the input is not a scalar but a 2xN matrix of N coordinates. And that it is the shape defined by these coordinates that is shifted 4 units down and 2 to the right. The output of this function is another 2xN matrix with the shifted coordinates.
The body of the provided code seems more complex than what the solution to the problem requires.
Answers (1)
Image Analyst
on 19 Apr 2014
Edited: Image Analyst
on 19 Apr 2014
It's trivial. Just repeat the code after adding 4 and subtracting 2:
function [Row1,Row2] = problem3(nSides)
% First the original, unshifted plot.
subplot(1, 2, 1);
r = 1;
theta = pi./nSides .* (1:2:2*nSides-1);
Row1 = r .* cos(theta);
Row2 = r .* sin(theta);
fill(Row1+4, Row2-2, 'k');
grid on;
xlim([0 9]);
ylim([-5, 0]);
axis equal;
% Now shift 4 units to the right and 2 units down.
subplot(1, 2, 2);
Row1 = Row1 + 4;
Row2 = Row2 - 2;
fill(Row1+4, Row2-2, 'k');
grid on;
xlim([0 9]);
ylim([-5, 0]);
axis equal;
Get rid of the subplot() calls if you want it all on one axes. Put a "hold on" in there if you don't want the second plot to blow away the first one.
1 Comment
Image Analyst
on 20 Apr 2014
Then nSides is a bad name for that input argument. When it's called nSides, you'd think it was the number of sides, and in fact your code does draw an n-sided polygon. So what should the inside of the function be? Should it just plot the coordinates:
x = nSides(:,1);
y = nSides(:, 2);
plot(x, y, 'bo-', 'LineWidth', 2, 'MarkerSize', 15);
hold on;
plot(x+4, y-2, 'bo-', 'LineWidth', 2, 'MarkerSize', 15);
Categories
Find more on Surface and Mesh Plots 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!