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:
time = ...
[2016 76 78 87 33 33 81 58 49 0 11 81 54 5 82 6;
2016 54 98 26 8 94 42 96 72 37 91 70 57 42 37 7;
2016 59 22 10 98 95 49 92 39 64 1 54 5 25 74 6;
2016 95 20 79 12 33 67 21 14 23 58 71 61 19 16 7;
2016 84 19 57 45 89 25 6 59 1 81 30 66 60 74 6;
2016 49 4 50 36 22 30 91 46 69 2 78 26 26 83 7;
2016 36 8 60 62 91 38 30 54 56 56 99 87 80 2 6;
2016 78 75 51 34 37 50 68 16 99 95 87 99 64 43 7;
2017 68 56 46 53 80 10 2 93 61 81 79 41 40 25 6;
2017 70 21 34 22 17 8 62 64 0 56 85 16 25 84 7;
2017 12 86 63 95 11 9 8 53 99 15 51 84 71 90 6;
2017 28 18 36 14 76 86 54 44 40 7 90 79 26 41 7;
2017 97 12 63 57 89 37 28 14 89 21 45 69 34 20 6;
2017 86 82 8 57 91 28 64 18 6 33 61 58 47 45 7;
2017 79 11 99 30 56 96 8 37 14 28 25 74 7 34 6;
2017 19 97 70 93 64 29 85 34 78 97 16 92 72 87 7];
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);
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)';
Y=Max';
y=Mean*ones(1,4);
cla
hold on
plot(X,y,'b')
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.
10 Comments
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_514489
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_514489
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_514490
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_514490
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_514731
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_514731
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_514756
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_514756
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515007
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515007
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515083
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515083
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515092
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515092
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515135
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515135
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515168
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515168
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515171
Direct link to this comment
https://la.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515171
Sign in to comment.