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

2 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Image Analyst
Image Analyst am 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);
  5 Kommentare
Pragya Dhungel
Pragya Dhungel am 27 Jan. 2022
I tried the code, it works for the bar plot but not for the line plot, i.e. plot(x,y1,x,y2,x,y3,x,y4) kind of command , where I want four different lines for each of the four function. Can we do that in a single line as we did for bar(theMeans) or code similar to @William Rose where the function are already seperated is only our option?
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
**apologies if you've already made it clear and I am still missing it.
William Rose
William Rose am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

William Rose
William Rose am 25 Jan. 2022
Bearbeitet: William Rose am 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
  3 Kommentare
William Rose
William Rose am 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
William Rose am 26 Jan. 2022
Bearbeitet: William Rose am 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')

Melden Sie sich an, um zu kommentieren.


William Rose
William Rose am 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 Kommentare
William Rose
William Rose am 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
Pragya Dhungel am 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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by