Storing info of an averaging automated process into an array without extra values
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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?
0 Kommentare
Akzeptierte Antwort
Voss
am 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)
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)
3 Kommentare
Walter Roberson
am 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.
Weitere Antworten (1)
Walter Roberson
am 4 Jul. 2022
A numeric array cannot store different number of values according to row. You would need a cell array for that.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!