Vectors Must be the Same Length
Mostrar comentarios más antiguos
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
el 10 de Dic. de 2017
You input Day but you ignore it and use the undefined hour instead
Rafael
el 10 de Dic. de 2017
Michal Dobai
el 11 de Dic. de 2017
What is stored in variable time ? Can you post here an example?
Walter Roberson
el 11 de Dic. de 2017
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 ?
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.
Walter Roberson
el 11 de Dic. de 2017
Maximum by rows would be
Max=max(time(Index,2:13),[],1)
Walter Roberson
el 11 de Dic. de 2017
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
el 11 de Dic. de 2017
Respuesta aceptada
Más respuestas (1)
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(:)))
Categorías
Más información sobre Argument Definitions en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



