Storing info of an averaging automated process into an array without extra values
Show older comments
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
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
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
More Answers (1)
Walter Roberson
on 4 Jul 2022
0 votes
A numeric array cannot store different number of values according to row. You would need a cell array for that.
Categories
Find more on Matrix Indexing 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!