I need some help in this problem
I have this matrix in MATLAB:
A = [ 25 1.2 1
28 1.2 2
17 2.6 1
18 2.6 2
23 1.2 1
29 1.2 2
19 15 1
22 15 2
24 2.6 1
26 2.6 2];
1st column is some measured values for temperature
2nd column is an index code representing the color (1.2:red,…..etc)
3rd column is the hour of taking the sample. Only at hours from 1 to 2
I want the matrix to be controlled by 2nd column as follows:
if it is 1.2, the program will find the average of all temperatures at hour 1 that
corresponds to 1.2
So, here ( 25 + 23 )/2 = 24
and also finds the average of all temperatures at hour 2 and that corresponds
to 1.2, ( 28 + 29 ) /2 = 28.5
and this average values:
[24
28.5]
will replace all temperature values at hours 1 and 2
that corresponds to 1.2 .
Then, it does the same thing for indices 2.6 and 15
So, the desired output will be:
B = [ 24
28.5
15.5
22
24
28.5
19
22
15.5
22]
My problem is in using the loop. I could do it for only one index at one run.
for example,
T=[];
index=1.2;
for i=1:length(A)
if A(i,2)==index
T=[T A(i,1)];
else
T=[T 0];
end
end
So, T is the extracted T that corresponds to 1.2 and other entries are zeros
Then, I wrote long code to find the average and at the end I could find the matrix
that corresponds to ONLY the index 1.2 :
B = [24
28.5
0
0
24
28.5
0
0
0
0]
But this is only for one index and it assigns zeros for the other indices. I can do this for all
indices in separate runs and then add the B’s but this will take very long time since my real
matrix is 8760 by 5 .
I am sure that there is a shorter way to do that.
Thanks
Regards
Try this:
Edit
I think your expected answer is wrong for
c = 2.6andt = 1… Shouldn’t it be(17 + 24)/2 = 20.5?