how to break large data into groups and run same steps in a loop for each group?

2 views (last 30 days)
Pragya Dhungel on 25 Jan 2022
Commented: William Rose on 28 Jan 2022
x= 1:100000000
data= [ sin(x), cos(x), tan(x), x]
how do I find mean of sin(x), cos(x), tan(x) and x in a loop (like M=mean(data1) and run this until I get mean of x) and plot each of these in a same plot ?
If I try to access data(1) it gives me a number and not an entire sin(x) value.

Image Analyst on 25 Jan 2022
Try this:
tic;
x = 1:100000000;
data= [ sin(x), cos(x), tan(x), x];
numElements = length(x);
theMeans = zeros(1, 4);
for k = 1 : 4
index1 = (k - 1) * numElements + 1;
index2 = k * numElements;
theData = data(index1 : index2);
theMeans(k) = mean(theData);
end
toc
Elapsed time is 36.811941 seconds.
theMeans % Show in command window.
theMeans = 1×4
1.0e+07 * 0.0000 0.0000 -0.0000 5.0000
% Obviously the means of the sin, cos, and tan will all be around 0
% while the mean of x will be gigantic. So you'll only see the last bar.
bar(theMeans);
grid on;
fontSize = 14;
title('Means', 'FontSize', fontSize);
xlabel('Segment', 'FontSize', fontSize);
ylabel('Mean Value', 'FontSize', fontSize);
William Rose on 28 Jan 2022
@Pragya Dhungel, Here is my understanding of what you want to do:
Array data has 4 long columns. Compute and plot the average of each column, using non-overlapping segments of length M:
N=1000; M=25; %N=column length, M=segment length
L=fix(N/M); %number of segments
%M does not need to be an integer divisor of N
x=(1:N)'*2*pi*4/N;
data=[sin(x),cos(x),tan(x),x];
means=zeros(L,4); %allocate array for the means
for i=1:L
for j=1:4
means(i,j)=mean(data(1+(i-1)*M:i*M,j));
end
end
figure;
subplot(211)
%disp([size(1:fix(N/M)),size(means(:,1))]);
plot(1:fix(N/M),means(:,1),'-r.',1:fix(N/M),means(:,2),'-g.',...
1:fix(N/M),means(:,3),'-b.');
legend('sin','cos','tan'); ylabel('Mean of Segment');
title('Columns 1,2,3 of data()');
subplot(212), plot(1:fix(N/M),means(:,4),'-k.');
xlabel('Segment Number'); ylabel('Mean of Segment')
title('Column 4 of data()');
I plot the means for column 4 separately, due to the very different scale.

William Rose on 25 Jan 2022
Edited: William Rose on 25 Jan 2022
The commands you gave create an array which is 1 row by 400000000 columns.
To illustrate, let us reduce the size of x:
x=1:10
x = 1×10
1 2 3 4 5 6 7 8 9 10
data=[sin(x), cos(x), tan(x), x];
fprintf('Size of data: %d by %d\n',size(data));
Size of data: 1 by 40
I expect this was not what you intended. Try this instead:
data=[sin(x); cos(x); tan(x); x];
fprintf('Size of data: %d by %d\n',size(data));
Size of data: 4 by 10
Each function is on a different row. Find the mean of each row:
disp(mean(data,2));
0.1411 -0.1417 -0.9016 5.5000
mean(data,2) computes the mean of each row. mean(data) computes the mean of each column, which is probably not what you want, for this array.
To compute and display the mean of row 1:
disp(mean(data(1,:)));
0.1411
To compute and display the mean of column 3:
disp(mean(data(:,3)));
0.5021
William Rose on 26 Jan 2022
The following code illustrates the arrays a, b, x used in the example above:
N=20;
x=zeros(N); %create N by N array
for i=1:N, for j=1:N, x(i,j)=2*(i+N*(j-1)-N^2/2)/N^2; end,end %fill array
a=sin(2*pi*x); b=cos(2*pi*x); %compute arrays a, b
figure;
subplot(3,1,1), surf(1:N,1:N,a); zlabel('a')
subplot(3,1,2), surf(1:N,1:N,b); zlabel('b')
subplot(3,1,3), surf(1:N,1:N,x); zlabel('x')

William Rose on 26 Jan 2022
N=20;
x=zeros(N); %create N by N array
for i=1:N, for j=1:N, x(i,j)=2*(i+N*(j-1)-N^2/2)/N^2; end,end %fill array
a=sin(2*pi*x); b=cos(2*pi*x); %compute arrays a, b
meana=mean(a); meanb=mean(b); meanx=mean(x); %compute means of the columns
plot(1:N,meana,'-rx',1:N,meanb,'-g+',1:N,meanx,'-b.'); %plot results
grid on; legend('mean(a)','mean(b)','mean(x)');
Pragya Dhungel on 26 Jan 2022
Yes. The data is too large and I am trying to break it down into groups and compute their mean. This discussion helped me a lot to understand it. Thanks!

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by