Error in the specification of the window when using movmad
Mostra commenti meno recenti
Hello,
I got a matrix called "A" [nxm], where n is the number of observations and m is the number of variables.
I want to perform a movmean and movmedian, but instead of use the size of the window kb and kf the same for all the variables, i created a matrix B [1xm], with the values of "k" to use for each variable of the matrix table
the code to do this should be?
n = 969120;
m = 41;
result1 = NaN(n,m);
result2 = NaN(n,m);
for j = 1:m
for j = 1:n
result1(i,j) = movmad(A(i,j),[B(1,j) B(1,j)],'omitnan');
result2(i,j) = movmedian(A(i,j),[B(1,j) B(1,j)],'omitnan');
end
end
because when i run this in a script, i get the following error:
"error using movmad, window length must be a finite positive scalar or 2-element vector of finite nonnegative scalars"
7 Commenti
Walter Roberson
il 6 Giu 2018
I recommend against using table as the name of a variable, as you are going to confuse readers who know that table() creates table data objects.
table(i,j) is going to be either a scalar numeric value or a scalar struct or a scalar cell array or a single row and column of a table data object (we do not know which because we did not see the initialization.) The only one of those that can have movmad() applied to it is the scalar numeric value, and movmad() is useless on scalar numeric values.
I speculate that your A values include non-integers or 0s or nan.
Tiago Dias
il 6 Giu 2018
Walter Roberson
il 6 Giu 2018
"it is just a name ok."
If you expect other people to read your code (such as if you are asking them to help debug your code) then it is highly recommended that you use names that are easy to understand, avoiding using variable names that are open to interpretation or which require close reading of all of the lines. Avoiding, in other words, using the names of any of the common functions as a variable name.
When you make code difficult for people to read, they might not bother to read it.
Tiago Dias
il 6 Giu 2018
Tiago Dias
il 6 Giu 2018
Walter Roberson
il 6 Giu 2018
You still have the difficulty that you appear to be apply movmad() and movmedian() to scalar numerics.
Tiago Dias
il 6 Giu 2018
Risposte (1)
Walter Roberson
il 6 Giu 2018
Modificato: Walter Roberson
il 6 Giu 2018
if any(isnan(B)) || any(B(<0)) || any(B~=floor(B))
error('B must be non-negative integers');
end
n = 969120;
m = 41;
result1 = NaN(n,m);
result2 = NaN(n,m);
for i = 1:n
result1(i,:) = movmad(A(i,:), [B(i) B(i)], 'omitnan');
result2(i,:) = movmedian(A(i,:), [B(i) B(i)], 'omitnan');
end
2 Commenti
Tiago Dias
il 6 Giu 2018
Walter Roberson
il 7 Giu 2018
if any(isnan(B)) || any(B(<0)) || any(B~=floor(B))
error('B must be non-negative integers');
end
n = 969120;
m = 41;
result1 = NaN(n,m);
result2 = NaN(n,m);
for i = 1:m
result1(:,i) = movmad(A(:,i), [B(i) B(i)], 'omitnan');
result2(:,i) = movmedian(A(:,i), [B(i) B(i)], 'omitnan');
end
Categorie
Scopri di più su RF Toolbox in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!