Error Using Plot Vectors Must Be The Same Lengths
Contents |
Support Answers MathWorks Search MathWorks.com MathWorks Answers Support MATLAB Answers™ MATLAB Central Community Home MATLAB Answers File Exchange Cody Blogs Newsreader Link Exchange ThingSpeak Anniversary Home Ask Answer Browse More how to make vectors the same length matlab Contributors Recent Activity Flagged Content Flagged as Spam Help MATLAB Central Community Home
How To Plot Vectors Of Different Length In Matlab
MATLAB Answers File Exchange Cody Blogs Newsreader Link Exchange ThingSpeak Anniversary Home Ask Answer Browse More Contributors Recent Activity Flagged how to use linspace in matlab Content Flagged as Spam Help Trial software alexr (view profile) 1 question 0 answers 0 accepted answers Reputation: 0 Vote0 Vectors must be the same lengths; plotting Asked by alexr alexr (view profile) error using plot not enough input arguments 1 question 0 answers 0 accepted answers Reputation: 0 on 2 May 2011 2,242 views (last 30 days) 2,242 views (last 30 days) I have googled this to death, the common error was people looping arrays. I just have 2 simple sets and cant see where the error is.simulated=[0.0000000 4.3830000 4.9470000 2.0180000 0.1761000 0.0162800 4.3650000 4.9540000 2.0170000 0.1760000 0.0162700 ] actual= [0.0750000 4.0750000 4.7630000 3.5130000
Plot Function In Matlab
0.5125000 0.1375000 2.6380000 4.5750000 4.8880000 1.5130000 0.3250000 ] x = 0:0.0002:0.01; plot(x, simulated, x, actual); legend('Simulated', 'Actual') ylabel('Current (mA)') xlabel('Time ( mS )') title('Current across R2 as a function of VS','FontSize',12) 3 Comments Show all comments Paulo Silva Paulo Silva (view profile) 14 questions 952 answers 365 accepted answers Reputation: 2,344 on 3 May 2011 Direct link to this comment: https://www.mathworks.com/matlabcentral/answers/6670#comment_13754 For the plot function to work x and y must have the same number of elements,your x vector got 51 elements and the simulated and actual vectors just 11. You can solve the problem by making the x vector with 11 elements instead of the 51 or by adding NaN values to the simulated vector like this simulated(end:end+numel(x)-numel(simulated))=nan; plot(x,simulated) (I couldn't post an answer to this question for some unknown reason, it gives me always one error) Andrew Newell Andrew Newell (view profile) 15 questions 717 answers 273 accepted answers Reputation: 1,832 on 3 May 2011 Direct link to this comment: https://www.mathworks.com/matlabcentral/answers/6670#comment_13756 To create an array x with the same span of values but the right size, you could do this: x = linspace(0,0.01,length(simulated)); alexr alexr (view profile) 1 question 0 answers 0 accept
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack how to plot in matlab Overflow the company Business Learn more about hiring developers or posting ads with us matlab subplot Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up MATLAB 'Error using plot Vectors must be the same length.' up vote 0 down vote https://www.mathworks.com/matlabcentral/answers/6670-vectors-must-be-the-same-lengths-plotting favorite x = 0:1:5; % define x array y = exp(x)+x.^4+2*x.^2-x+3; % define y array dx = diff(x); % 1 1 1 1 1 dx has one number less than x dy = diff(y); % -5 -1 -3 -4 -2 slope = dy./dx z=exp(x)+4*x.^3 +4*x-1 plot(x,slope,'*',x,z) 'Error using plot Vectors must be the same length.' Been stuck on this for a while, I am not sure what to do. ive seen http://stackoverflow.com/questions/28898818/matlab-error-using-plot-vectors-must-be-the-same-length the other responses to the same error but can not understand the code. It seems its the colon : that might help but i am not sure how to use it. Any help would be appreciated! matlab vector share|improve this question asked Mar 6 '15 at 12:29 abbeywell 11 2 The problem is that the vectors must be the same length. x and slope do not have the same amount of elements. How are you supposed to plot them if there is not an x-y pair? –Ander Biguri Mar 6 '15 at 12:31 add a comment| 1 Answer 1 active oldest votes up vote 1 down vote Since slope is computed from two consecutive values, I suggest you take the average betwen consecutive points as the x-axis values for ´slope`: plot((x(1:end-1)+x(2:end))/2,slope, '*',x,z) share|improve this answer answered Mar 6 '15 at 12:34 Luis Mendo 73.2k83886 ahhh thanks a lot. I have another problem now, how would I plotting the differences between the two solutions on another graph? the x values dont have the same amount of values.. –abbeywell Mar 6 '15 at 13:37 You could apply interp1 to x and y to obtain the midway values, and then subtract –Luis Mendo Mar 6 '15 at 20:3
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us http://stackoverflow.com/questions/18529355/error-using-plot-vectors-must-be-the-same-lengths-for-vectors-of-same-len Learn more about Stack Overflow the company Business Learn more about hiring developers http://stackoverflow.com/questions/21262876/vectors-must-be-the-same-length-error or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up “Error using ==> plot Vectors must be the how to same lengths.” for vectors of same length up vote 1 down vote favorite I have a problem with this error. I have two matrices, days1 and days2, with size: [size(days1) size(days2)] ans = 14749 2 14749 2 So they have the same length and depth. However, when I try to plot the two graphs together, I get the following error: plot(days1(1,:),days1(:,2),'b',days2(1,:),days2(:,2),'r') ??? Error using ==> plot Vectors error using plot must be the same lengths. What's the reason? Thanks in advance. matlab plot share|improve this question asked Aug 30 '13 at 9:22 aliants 4716 add a comment| 2 Answers 2 active oldest votes up vote 1 down vote accepted The vectors you are plotting are not the same length: >> size(days1(1,:)) ans = 1 2 >> size(days1(:,2)) ans = 14749 1 share|improve this answer answered Aug 30 '13 at 9:27 am304 10.9k21225 You are right!! I have to do plot(days1(:,1),days1(:,2),'b',days2(:,1),days2(:,2),'r') Thanks a lot!! –aliants Aug 30 '13 at 9:29 add a comment| up vote 1 down vote That is because: days1(1,:) days1(:,2) ^ [1 x 2] ^ [14749 x 1] which are obviously NOT the same length :) share|improve this answer answered Aug 30 '13 at 9:27 Rody Oldenhuis 28.4k62579 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email and Password Post as a guest Name Email Post as a guest Name Email discard By posting your answer, you agree to the privacy policy and terms of service. Not the answer you're looking for? Browse other questions tagged matl
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up ' vectors must be the same length' error up vote 0 down vote favorite I've got a 250 x 250 image, I want to have a scatter plot of the intensity of every pixel and its nearest neighborhood. This is my code: I = imread(image); i = [1,249]; j = [1,250]; X = I(i,j); Y = I(i+1,j); scatter(X,Y); why do I get the " X and Y vectors must be the same length" error? They are the same length ! matlab scatter share|improve this question asked Jan 21 '14 at 15:50 user3200664 514 add a comment| 2 Answers 2 active oldest votes up vote 1 down vote Because scatter(X, Y) is only used for vectors, not matrix. In your example, both X and Y are 2x2 matrices, not vectors. From its documentation: scatter(X,Y) displays circles at the locations specified by the vectors X and Y. This type of graph is also known as a bubble plot. Edit: if you want to plot matrix, use plotmatrix() instead: plotmatrix(X,Y) share|improve this answer answered Jan 21 '14 at 16:13 herohuyongtao 25.5k96792 add a comment| up vote 0 down vote Scatter(X,Y) is used only for vectors as herohuyongtao correctly mentioned. You could try to do the following: m = 250; X = I(m+1:end); Y = I(1:end-m); scatter(X,Y); You convert your image matrix I into a vector X while ignoring the first column and in a vector Y while ignoring the last column. X(n) is thus the neighbour of Y(n) on the right side. I hope this helps! share|improve this answer answered Jan 21 '14 at 16:46 Pietertje 314 add a comment| Your Answe