Storing info of an averaging automated process into an array without extra values

1 view (last 30 days)
I have a code that automates the process of averaging elements into one value for each set of values until the end of an array, for example it takes the average of every 2 elements of a full array, then it takes the average of every 3 elements, then 4, etc. That info is then stored into a single array ma. I want to be able to log the info in regards to k, where with ma(n,k) we get a 17x9 array which is close to what I want, however the array includes a ton of unwanted zeroes/values. For example:
clear all
mdata = [1:10];
n1 = 1;
n = 1;
for k = 1:length(mdata) - 1
n1 = 1;
while n1 + k <= length(mdata)
ma(n,k) = (1/(k + 1)) * sum(mdata(n1:n1 + k));
n1 = (n1 + k) + 1;
std_ma = std(ma);
n = n + 1;
end
end
ma
ma = 17×9
1.5000 0 0 0 0 0 0 0 0 3.5000 0 0 0 0 0 0 0 0 5.5000 0 0 0 0 0 0 0 0 7.5000 0 0 0 0 0 0 0 0 9.5000 0 0 0 0 0 0 0 0 0 2.0000 0 0 0 0 0 0 0 0 5.0000 0 0 0 0 0 0 0 0 8.0000 0 0 0 0 0 0 0 0 0 2.5000 0 0 0 0 0 0 0 0 6.5000 0 0 0 0 0 0
I then included a line that turns all zeros into NaN in the next code, however I was wondering if it is possible for only the averaged values to be included, without all the unnecessary values in the same column array? I don't want any of the extra zeroes or NaN, just the values themselves in the same formatted array. How would I go about this?
clear all
mdata = [1:10];
n1 = 1;
n = 1;
for k = 1:length(mdata) - 1
n1 = 1;
while n1 + k <= length(mdata)
ma(n,k) = (1/(k + 1)) * sum(mdata(n1:n1 + k));
ma(ma == 0) = nan;
n1 = (n1 + k) + 1;
std_ma = std(ma);
n = n + 1;
end
end
ma
ma = 17×9
1.5000 NaN NaN NaN NaN NaN NaN NaN NaN 3.5000 NaN NaN NaN NaN NaN NaN NaN NaN 5.5000 NaN NaN NaN NaN NaN NaN NaN NaN 7.5000 NaN NaN NaN NaN NaN NaN NaN NaN 9.5000 NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.0000 NaN NaN NaN NaN NaN NaN NaN NaN 5.0000 NaN NaN NaN NaN NaN NaN NaN NaN 8.0000 NaN NaN NaN NaN NaN NaN NaN NaN NaN 2.5000 NaN NaN NaN NaN NaN NaN NaN NaN 6.5000 NaN NaN NaN NaN NaN NaN
I want something like this as an array output for each set of n averaged values of an array:
1.5 2.0 2.5 etc. etc.
3.5 5.0 6.5
5.5 8.0
7.5
8.5
Is this possible?

Accepted Answer

Voss
Voss on 4 Jul 2022
"I was wondering if it is possible for only the averaged values to be included, without all the unnecessary values in the same column array?"
Storing everything in one column vector, as I think you are asking for:
mdata = [1:10];
n1 = 1;
n = 1;
for k = 1:length(mdata) - 1
n1 = 1;
while n1 + k <= length(mdata)
% ma(n,k) = (1/(k + 1)) * sum(mdata(n1:n1 + k)); % don't store this result in column k
ma(n,1) = (1/(k + 1)) * sum(mdata(n1:n1 + k)); % store it in column 1
n1 = (n1 + k) + 1;
std_ma = std(ma);
n = n + 1;
end
end
disp(ma)
1.5000 3.5000 5.5000 7.5000 9.5000 2.0000 5.0000 8.0000 2.5000 6.5000 3.0000 8.0000 3.5000 4.0000 4.5000 5.0000 5.5000
However, it seems likely that you might need to know which value of k each average value is derived from, in which case one thing you can do is to make ma a cell array and store a numeric column vector of averages in each cell of ma:
mdata = [1:10];
n1 = 1;
n = 1;
N = numel(mdata)-1;
ma = cell(N,1);
for k = 1:N
n1 = 1;
while n1 + k <= length(mdata)
ma{k}(end+1,1) = (1/(k + 1)) * sum(mdata(n1:n1 + k));
n1 = (n1 + k) + 1;
% std_ma = std(ma); % this would need to be modified
n = n + 1;
end
end
disp(ma)
{5×1 double} {3×1 double} {2×1 double} {2×1 double} {[ 3.5000]} {[ 4]} {[ 4.5000]} {[ 5]} {[ 5.5000]}
  3 Comments
Walter Roberson
Walter Roberson on 5 Jul 2022
Alternate code that might be clearer:
ma{k}(end+1,1) = mean(mdata(n1:n1+k));
The end+1 is talking about where to store the result, and does not have to do with the calculation itself.
end+1 means "the next location after the current last location". It is used to append data on to the end of an existing vector.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 4 Jul 2022
A numeric array cannot store different number of values according to row. You would need a cell array for that.

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!