excel averageif function in matlab

4 views (last 30 days)
for cata = 1:281
mydata = mean(cellfun(@(x) x(cata,2), thecells));
data2 = cell2mat(num2cell(mydata));
plot(cata,data2)
end
mydata is 5000*2 double look like
time data
1 225.42968
1 453.4097824
1 254.1819706
2 164.3800456
2 411.2133781
2 266.0093299
2 399.3169552
3 155.2650000
4 322.7854587
4 366.5487525
i try many methods but cannot create a loop same as averageif function in excel. my prof tell me to put nan for numbers to create same size for each time. But I am wondering another ways of the averageif function in excel.

Accepted Answer

Star Strider
Star Strider on 29 Jun 2018
I am not certain what structure your original data are.
Try this:
C = {1 225.42968
1 453.4097824
1 254.1819706
2 164.3800456
2 411.2133781
2 266.0093299
2 399.3169552
3 155.2650000
4 322.7854587
4 366.5487525};
M = cell2mat(C);
Out = accumarray(M(:,1), M(:,2), [], @mean);

More Answers (1)

Steven Lord
Steven Lord on 29 Jun 2018
If you're using release R2018a and your data is stored as a table, use groupsummary.
time = [1; 1; 1; 2; 2; 2; 2; 3; 4; 4];
data = [225.42968;
453.4097824;
254.1819706;
164.3800456;
411.2133781;
266.0093299;
399.3169552;
155.2650000;
322.7854587;
366.5487525];
T = table(time, data)
groupsummary(T, 'time', 'mean')
Or you could use splitapply (since your times are already group numbers; if they weren't a vector of positive integer values you would need to use findgroups first.)
meantime = splitapply(@mean, data, time)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!