how to break large data into groups and run same steps in a loop for each group?
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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
theMeans % Show in command window.
% 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);
5 Comments
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.
More Answers (2)
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
data=[sin(x), cos(x), tan(x), x];
fprintf('Size of data: %d by %d\n',size(data));
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));
Each function is on a different row. Find the mean of each row:
disp(mean(data,2));
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,:)));
To compute and display the mean of column 3:
disp(mean(data(:,3)));
3 Comments
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)');
This seems to be what you requested.
William Rose
on 26 Jan 2022
Edited: 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
[I have moved this answer from the Comments to the Answers.]
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)');
3 Comments
William Rose
on 26 Jan 2022
@Image Analyst is exactly right, as usual.
@Pragya Dhungel, I wonder if you have a very long 1D array, and you want to compute the mean of it, one segment at a time. Perhaps you are thinking that by reshaping it into a rectangular array, you could compute the mean of each column. That could explain why you started with a 1D array, and then changed to 2D.
If you are trying to compute the means of successive segments of a long vector, then you are right: you could do it by reshaping, then computing the column means.
See Also
Categories
Find more on Graphics Performance in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!