Rafael
さんによって質問されました 2017 年 12 月 10 日

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.

Answer by Michal Dobai
on 12 Dec 2017

Edited by Michal Dobai
on 12 Dec 2017

Accepted Answer

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);

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.

Rafael
on 12 Dec 2017

Michal Dobai
on 12 Dec 2017

I'm glad you managed to solve your problem :)

Rafael
on 12 Dec 2017

Hey, thank you! You are a master!

サインイン to comment.

Answer by Jos (10584)
on 11 Dec 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(:)))

Rafael
on 11 Dec 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
on 12 Dec 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.

サインイン to comment.

Opportunities for recent engineering grads.

Apply Today
## 10 件のコメント

## Walter Roberson (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_514489

## Rafael (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_514490

## Michal Dobai (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_514731

## Walter Roberson (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_514756

## Rafael (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515007

## Michal Dobai (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515083

## Walter Roberson (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515092

## Rafael (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515135

## Walter Roberson (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515168

## Rafael (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/372119-vectors-must-be-the-same-length#comment_515171

サインイン to comment.