How can I average pairs of numbers within a vector?

1 Ansicht (letzte 30 Tage)
Josh Huang
Josh Huang am 17 Nov. 2017
Bearbeitet: Aarti Soni am 11 Jul. 2023
I have two for loops here, and my output vector isn't quite what I need. How can I average two values within this loop?
deg= [-20,-175,-15,-125,-10,-5,0,5,10];
run= [1,2];
n=1;
for i= 1:length(deg)
for ii=1:length(run)
%
filename= ['q',num2str(deg(i)),'deg_2winglet_run',num2str(run(ii)),'.mat'];
temp= load(filename);
Fz(n)=mean(temp.F(:,3));
n=n+1;
end
end
Right now I am outputting a vector of the form [1_run1,1_run2,2_run1,2_run2,...] and I want to average the runs. I want the output vector to become [(1_run1)+(1_run2)/2, (2_run1)+(2_run2)/2, ...]. Is there a way to put this inside the for loops?
  2 Kommentare
per isakson
per isakson am 17 Nov. 2017
Bearbeitet: per isakson am 17 Nov. 2017
After reading only the title
>> vec = (1:12)
vec =
1 2 3 4 5 6 7 8 9 10 11 12
>> vec = reshape( vec, 2,[] )
vec =
1 3 5 7 9 11
2 4 6 8 10 12
>> mean( vec, 1 )
ans =
1.5000 3.5000 5.5000 7.5000 9.5000 11.5000
>>
Josh Huang
Josh Huang am 17 Nov. 2017
wow, thanks! really appreciate it

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Aarti Soni
Aarti Soni am 11 Jul. 2023
Bearbeitet: Aarti Soni am 11 Jul. 2023
What if the data is three dimensional?
for example: latitute X longitute X 60
how to take average in pair to make it latitute X longitute X 30?

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by