how to break large data into groups and run same steps in a loop for each group?
Show older comments
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.
Accepted Answer
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
Pragya Dhungel
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')
[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
Image Analyst
on 26 Jan 2022
Your initial question said nothing about having x be a 2-D array. You defined it as a 1-D array going from 1 to 100 million.
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.
Pragya Dhungel
on 26 Jan 2022
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!



