Vectors Must be the Same Length

I want to plot this but the problem is that I need to somehow have the vectors be the same length.
Year=input('Enter the year ');
Hour=input('Enter the hour ');
Index=find(time(:,1)==Year & time(:,16)==Hour);
Max=max(time(Index,2:13))
Mean=mean(mean(time(Index,2:13)));
X=(1:365)';
Y=Max';
y=Mean*ones(1,365);
cla
hold on
plot(X,y,'b') %This plots the mean value with x.
plot(X,Y)
The objective is to plot max and mean when x goes from 1 to 365. Mean has to be a single line because it's a single value. Max has to be 12x365 and it should return only one value for each column but the problem is that when I run that code it gives me the error.
Day=input('Enter the year ');
Year=input('Enter the hour ');
Index=find(time(:,1)==Year & time(:,16)==Hour);
Max=max(time(Index,2:13),1)
Mean=mean(mean(time(Index,2:13)));
X=(1:365)';
Y=Max';
y=Mean*ones(1,365);
cla
hold on
plot(X,y,'b') %This plots the mean value with x.
plot(X,Y)
If I run the above code it works fine. It doesn't return any error. Now the problem with the second one is that it returns more than one value for each column and the plot shows different colors in the graph. I want it to show only one color and hopefully run the code which returns one value for each column for max.

10 comentarios

Walter Roberson
Walter Roberson el 10 de Dic. de 2017
You input Day but you ignore it and use the undefined hour instead
Rafael
Rafael el 10 de Dic. de 2017
I'm sorry, that was supposed to be Day not Minute.
Michal Dobai
Michal Dobai el 11 de Dic. de 2017
What is stored in variable time ? Can you post here an example?
Please confirm that for your statement
Max=max(time(Index,2:13),1)
you deliberately mean that at each point, it is to take the maximum of the value 1 together with time(Index,2:13), returning an array the same size as time(Index,2:13) ?
As opposed to having intended to ask for the maximum along each column, returning something that would be a vector of length 12 ?
Rafael
Rafael el 11 de Dic. de 2017
Editada: Rafael el 11 de Dic. de 2017
Well, time is a matrix. I am gathering information from the matrix and plotting it into the graph.
Max=max(time(Index,2:13),1) this code takes whatever I typed from the input and finds anything similar in Index. Then this code looks for the maximum from Index columns 2 to 13.
The 1 returns more than one max value for each column. I if use Max=max(time(Index,2:13)) instead it returns only one value for each column but the problem is that it returns the error, vectors not the same length.
For the mean, I am supposed to get a line going up and down for Max and mean has to be a straight line.
Michal Dobai
Michal Dobai el 11 de Dic. de 2017
"The 1 returns more than one max value for each column."
Well, yeah. In fact it returns ALL of your selected values. It returns whole time(Index,2:13), but all values smaller than 1 are replaced by 1. So values you get aren't max values for each column, there are simply values greater than or equal to 1.
Now, I understand you don't want that; but I still don't understand what is the result you want to achieve. Because, yeah, when you try to plot X (of size 365x1) and Y (of size 12x1) there's no surprise, that MATLAB will tell you Vectors must be the same length. :)
It will be much easier to help you solve your problem if you post here an example of your data and (at least some draft) of graph you want to achieve.
Maximum by rows would be
Max=max(time(Index,2:13),[],1)
Rafael
Rafael el 11 de Dic. de 2017
Editada: Rafael el 11 de Dic. de 2017
When I use the [] is when I get the error,
Error using plot
Vectors must be the same length.
When you use
Max=max(time(Index,2:13),[],1)
you will get one result for each entry in Index. Then
X=(1:365)';
Y=Max';
plot(X, Y)
assumes that Y is 365 entries, which assumes that your Index happened to match exactly 365 locations every time. That seems unlikely to be the case as your Index is found by selecting on Day and Hour, so it seems unlikely that you would just happen to have 365 years to match the Day and Hour to.
If you have one year of data taken once per hour, it would make more sense for Index to be based upon Hour alone, giving you one result per day.
Question: what are you doing about leap year?
Question: Are the times local timezone or UTC? Because if they are local then you have to worry about any daylight savings time in effect.
Rafael
Rafael el 11 de Dic. de 2017
Sorry, Days is supposed to be Years. I fixed it.
I have to plot X that is 1:365 (which is days) vs Y has to be the Max from maximum value of each day for the inputted year. That is supposed to give me something like this,

Iniciar sesión para comentar.

 Respuesta aceptada

Michal Dobai
Michal Dobai el 12 de Dic. de 2017
Editada: Michal Dobai el 12 de Dic. de 2017
It's a lot more clear to me now. Or at least I hope so :) (reference to your comment )
Let's say that you have these input data:
%Year < < < this is your data > > > hour (day)
%-----------------------------------------------------------------------
time = ...
[2016 76 78 87 33 33 81 58 49 0 11 81 54 5 82 6; % 1
2016 54 98 26 8 94 42 96 72 37 91 70 57 42 37 7; % 1
2016 59 22 10 98 95 49 92 39 64 1 54 5 25 74 6; % 2
2016 95 20 79 12 33 67 21 14 23 58 71 61 19 16 7; % 2
2016 84 19 57 45 89 25 6 59 1 81 30 66 60 74 6; % 3
2016 49 4 50 36 22 30 91 46 69 2 78 26 26 83 7; % 3
2016 36 8 60 62 91 38 30 54 56 56 99 87 80 2 6; % 4
2016 78 75 51 34 37 50 68 16 99 95 87 99 64 43 7; % 4
2017 68 56 46 53 80 10 2 93 61 81 79 41 40 25 6; % 1
2017 70 21 34 22 17 8 62 64 0 56 85 16 25 84 7; % 1
2017 12 86 63 95 11 9 8 53 99 15 51 84 71 90 6; % 2
2017 28 18 36 14 76 86 54 44 40 7 90 79 26 41 7; % 2
2017 97 12 63 57 89 37 28 14 89 21 45 69 34 20 6; % 3
2017 86 82 8 57 91 28 64 18 6 33 61 58 47 45 7; % 3
2017 79 11 99 30 56 96 8 37 14 28 25 74 7 34 6; % 4
2017 19 97 70 93 64 29 85 34 78 97 16 92 72 87 7]; % 4
In this example I will assume that year has only four days and that you have somehow collected data for hours 6 and 7 for each day.
Now, when you input 2017 as year and 6 as hour, this
Index=find(time(:,1)==Year & time(:,16)==Hour);
will select exactly four rows from time matrix. (well, at least in my case, because I 'collected' values for every single day of the year 2017 at 6'o clock).
So now we can create selection of size 4x12 (in your case 365x12) like you already did: time(Index,2:13) Maximum of each day (each row of this selection) can be calculated like this:
Max = max(time(Index,2:13),[],2);
Why 2? From Documentation for function max:
M = max(A,[],dim) returns the largest elements along dimension dim. For example, if A is a matrix, then max(A,[],2) is a column vector containing the maximum value of each row.
Now max will be the column vector of size 4 (one value for each day of the year). X is also column vector of size 4, so we can plot it without any problem. (and without error Vectors Must be the Same Length)
Here is the whole code:
Year=input('Enter the year ');
Hour=input('Enter the hour ');
Index=find(time(:,1)==Year & time(:,16)==Hour);
Max = max(time(Index,2:13),[],2);
Mean = mean(mean(time(Index,2:13)));
X=(1:4)'; % In this example - year has only 4 days
Y=Max';
y=Mean*ones(1,4); % In this example - year has only 4 days
cla
hold on
plot(X,y,'b') %This plots the mean value with x.
plot(X,Y)
ylim([0,100]);
And here is the plot this code produce for input values Year=2017 and Hour=6:
Now, as Walter Roberson already mentioned in his comment , there can be cases when your code doesn't select exactly 365 rows. You should consider these cases and handle them properly.

22 comentarios

Rafael
Rafael el 12 de Dic. de 2017
That my friend just fixed my problem. You are a master!
Also, do you understand exactly what it is asking for Mean? I assumed Mean has to be a straight line or I am not sure if it has to be the average of each column.
This is what I'm trying to do,
  • Plot the hourly maximum and hourly mean time (minutes) for Year___ and Hour___.
  • The X axis should be numbered 1 to 365 and the Y axis should be scaled from - to the maximum value that appears in the data.
Michal Dobai
Michal Dobai el 12 de Dic. de 2017
...Plot the hourly maximum and hourly mean time (minutes)...
Hourly means hourly. So you should plot mean time the same way as maximum value.
Now, couple more things:
  • First of all, I'm glad that we managed to solve your problem, BUT. It took me a really long time to fully understand what you are trying to achieve. I understand, that this is some kind of homework or maybe your school project, and that you was maybe trying to avoid post here anything else other than your code, but next time try to give an example of input and output that you are hoping for. It doesn't have to be exact same data from your assignment, it can be and should be something simple that sufficiently demonstrate your intentions. If your program can't plot anything (because of error), you can always open paint and draw how should your plot look ;)
  • Try to fully understand basic concept of MATLAB Matrices and Arrays. Use documentation. MATLAB has awesome documentation with lots of examples you can just copy-paste and try. It can save you - and everyone who is starting to learn MATLAB - a lot of time.
  • And last thing. I personally think Cody can help beginners a lot. "Cody™ is a MATLAB Central game that challenges and expands your knowledge of MATLAB and Simulink®." You can try it. There are some really fun and interesting problems from complete basics to advanced.
That's all. If something is still not clear to you, you can ask here, I'll try to explain it to you in further detail. If you tried this solution, and it works as expected, you could consider to accept this answer :)
Rafael
Rafael el 12 de Dic. de 2017
Editada: Rafael el 12 de Dic. de 2017
Oh, no. It is not the same exact data. I took some things out of this.
  • "Hourly means hourly. So you should plot mean time the same way as maximum value."
I tried doing the exact thing with Mean
Max=max(data_2(Index,2:13),[],2);
Mean=mean(data_2(Index,2:13),[],2);
X=(1:365)';
Y1=Max';
Y2=Mean';
and it only returns,
Error using sum
Invalid option. Option must be 'double', 'native', 'default', 'omitnan' or 'includenan'.
Error in mean (line 117)
y = sum(x, dim, flag)/size(x,dim);
OK. So that means, there is obviously something wrong on that line
Mean=mean(data_2(Index,2:13),[],2);
Let's try to figure out what. MATLAB told you 'Error in mean'. Well you can try to look into the documentation of mean. Little hint - you are maybe looking for this: M = mean(A,dim) Are you using it right?
Rafael
Rafael el 12 de Dic. de 2017
I assumed I am using it right. You told me to used it exactly like in Max. Not sure why is it returning that error. Like line 117, that is not even in there, wow.
Michal Dobai
Michal Dobai el 12 de Dic. de 2017
Yes, I told you 'you should plot mean time the same way as maximum value.' That doesn't mean, however, that max() and mean() are exact same and use exact same input arguments. They are two different things :)
Have you looked at that documentation? Don't be frustrated, I'm sure you can figure it out. You are really trying to do the same thing. Calculate mean for each row of matrix. But you have to use mean() function right.
Rafael
Rafael el 12 de Dic. de 2017
Editada: Rafael el 12 de Dic. de 2017
Yes, like I show you above. I plot mean the same way as maximum. That's when I get the error. Also, I'm not frustrated, I'm just nervous. I need to learn how to do this for tomorrow and it doesn't matter how, just to show that it returns what's asked.
For the documentation, I tried looking at it. I don't really understand it.
Also, it's supposed to be time not data_2. That doesn't fix the issued if that is what you think is the problem.
OK, I give up. You was looking for this:
M = mean(A,dim) returns the mean along dimension dim. For example, if A is a matrix, then mean(A,2) is a column vector containing the mean of each row.
So that means you have to change this
Mean=mean(data_2(Index,2:13),[],2);
to this
Mean=mean(data_2(Index,2:13),2);
I already written this comment, so I'll just post it, maybe it will help the others...
And as for the ' line 117'. It says: Error in mean (line 117)
mean is MATLAB function. It has it's own code stored in it's own file. In fact, when you click on text (line 117) in your command window, MATLAB opens it for you.
Whole error info looks like this:
Error using sum
Invalid option. Option must be 'double', 'native',
'default', 'omitnan' or 'includenan'.
Error in mean (line 117)
y = sum(x, dim, flag)/size(x,dim);
Error in something (line 41) % 'something' is name of my file
Mean = mean(time(Index,2:13),[],2);
MATLAB is trying o tell you:
  • Error using sum - I was trying to sum elements of this matrix for you, but I couldn't do it, because: Invalid option. Option must be 'double', 'native', 'default', 'omitnan' or 'includenan'. (you don't have to fully understand what happened here, this error happened INSIDE mean code)
  • Error in mean (line 117) - MATLAB is saying: 'It happened here!, Right on this line (117 :) )', when I was trying to do this: y = sum(x, dim, flag)/size(x,dim);. This is NOT your code. It's MATLAB's. ...and there is nothing wrong with it. So that means error has to be caused by something else.
  • Error in something (line 41) - THIS is your code. (or mine, to be accurate). It means that in your code, you tried to do this: Mean = mean(time(Index,2:13),[],2);, MATLAB tried to execute this command, called function mean(), and got an error on line 117 (in that function), where it was trying to execute sum.
Rafael
Rafael el 12 de Dic. de 2017
That solved the problem for the error, although the graph is incorrect. It was supposed to be similar to Max, going up and down. Now is a bunch of straight line, thick, thin lines everywhere.
Michal Dobai
Michal Dobai el 12 de Dic. de 2017
Editada: Michal Dobai el 12 de Dic. de 2017
No, it's not. This:
Year=input('Enter the year ');
Hour=input('Enter the hour ');
Index=find(time(:,1)==Year & time(:,16)==Hour);
Max = max(time(Index,2:13),[],2);
Mean = mean(time(Index,2:13),2);
X=(1:4)';
Y1=Max';
Y2=Mean';
cla
hold on
plot(X,Y1,'b')
plot(X,Y2)
Gives me this
Which is correct. Bold line (represents maximum) above regular line (represents mean).
Now it's all about guessing, just like at the beginning. I don't have your code, I don't have your data. This is the working example. I hope it will help you.
Rafael
Rafael el 12 de Dic. de 2017
Editada: Rafael el 12 de Dic. de 2017
This is what I get. I am supposed to get something similar to Max (red).
Michal Dobai
Michal Dobai el 12 de Dic. de 2017
Please copy-paste your code here. Current version.
Rafael
Rafael el 12 de Dic. de 2017
Ride=input('Enter the ride number: ');
Year=input('Enter the year: ');
Index=find(data_2(:,1)==Ride & data_2(:,16)==Year);
Max=max(data_2(Index,2:13),[],2);
Mean=mean(data_2(Index,2:13),2);
X=(1:365)';
Y1=Max';
Y2=Mean'.*ones(365,1);
cla
one=plot(X,Y2,'b');
two=plot(X,Y1,'r');
Change this:
Y2=Mean'.*ones(365,1);
to this
Y2=Mean';
Also transpose (' operator) isn't necessary, here but result will be the same.
Rafael
Rafael el 12 de Dic. de 2017
Error using matlab.graphics.chart.primitive.Line/get
Invalid or deleted object.
Error in uistack (line 61)
Parent = get(Handles, {'Parent'});
Michal Dobai
Michal Dobai el 12 de Dic. de 2017
Editada: Michal Dobai el 12 de Dic. de 2017
That has nothing to do with previously mentioned line.
And please copy here the whole error message. Everything red in command window. On which line MATLAB gives you this error? Have you changed or added something since last comment?
And I noticed you deleted hold on command. If you want to have these two plots in one figure, you should keep it.
Rafael
Rafael el 12 de Dic. de 2017
Editada: Rafael el 12 de Dic. de 2017
That's exactly the error it returns when I changed Y2=Mean'*ones(1,365); to Y2=Mean';
Michal Dobai
Michal Dobai el 12 de Dic. de 2017
Means? With s at the end? Why?
And how exactly do you run your code? Do you have it in .m file? Or do you copy-paste it to command window every time?
Because if yes, that's why you don't know which line gives you error. Please create .m file and run your program by clicking on the Run button (or alternatively F5 key). Then you can - and should - use MATLAB debugging tools to figure out what's wrong with your code.
Rafael
Rafael el 12 de Dic. de 2017
Editada: Rafael el 12 de Dic. de 2017
I fixed it. I had an extra line, uistack which I didn't saw and once I removed it it return both graphs like I wanted.
Michal Dobai
Michal Dobai el 12 de Dic. de 2017
I'm glad you managed to solve your problem :)
Rafael
Rafael el 12 de Dic. de 2017
Hey, thank you! You are a master!

Iniciar sesión para comentar.

Más respuestas (1)

Jos (10584)
Jos (10584) el 11 de Dic. de 2017
Is this what you want?
X = 1:50 ;
Y = rand(size(X)) ;
Ymean = mean(Y(:)) ; % get single value; prefered over mean(mean(..))
plot(X, Y, 'ro') ;
hold on ;
plot(X([1 end]), Ymean([1 1]), 'b-') ;
There are also utilities on the File Exchange to plot horizontal and vertical lines at a specific value, like my own GRIDXY function, available here: https://uk.mathworks.com/matlabcentral/fileexchange/9973-gridxy--v2-2-feb-2008-
plot(X, Y, 'ro')
gridxy([], mean(Y(:)))

2 comentarios

Rafael
Rafael el 11 de Dic. de 2017
There has to be two plot.
  • X has to go from 1:365
  • Y is the Max
  • The other "y" is Mean and that plot is working. The one not working is with the Max.
Rafael
Rafael el 12 de Dic. de 2017
Yup, I remember now. The mean has to look like Max. I can't be a straight line. I have to set Mean like this,
Mean=mean(data_2(Index,2:13))
y=Mean.*ones(1,365);
The problem is that it returns Matrix dimensions must agree.

Iniciar sesión para comentar.

Categorías

Más información sobre Argument Definitions en Centro de ayuda y File Exchange.

Preguntada:

el 10 de Dic. de 2017

Comentada:

el 12 de Dic. de 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by