smoothdata function not working for matrix . any one have ideas to what is going on?

6 visualizaciones (últimos 30 días)
I attached a 60x22 matrix a.
I have a cell array containing a matrix like this one. I am trying to use the smoothdata function to smooth each column, but weirdly, it isn't working as intended (or at all).
For example, I can run:
a_smooth = smoothdata(a)
or
a_smooth = smoothdata(a,1)
both of which should smooth each column but nothing changes in the matrix. If I run a vector, it works fine.
Ultimately, I will use the following for a cellarray containing a matrix in each cell:
smooth_cell = cellfun(@smoothdata, a, 'UniformOutput',false)
Any insight is appreciated. Thanks!

Respuestas (2)

Star Strider
Star Strider el 23 de Sept. de 2021
Try this —
LD = load('mat.mat');
a = LD.a;
for k = 1:size(a,2)
a_smoothed(:,k) = smoothdata(a(:,k));
end
x = 1:size(a,1);
figure
plot(x, a_smoothed)
grid
legend(compose('Column %2d',1:size(a,2)), 'Location','eastoutside', 'NumColumns',2 ) % Optional
.
  5 comentarios
Star Strider
Star Strider el 23 de Sept. de 2021
Editada: Star Strider el 23 de Sept. de 2021
My pleasure!
I’m curious, too. Apparently, no one else ever tried that, or it’d have been reported and fixed by now.
EDIT — (23 Sep 2021 at 15:34)
@BobbyRoberts — FWIW, I checked ‘a’ for NaN and values (there were none) and cast it as double, checked it for every other problem I could think of (it plots correctly on its own), and still could not get it to work with smoothdata. That’s the reason I submitted the Service Request.
.
Star Strider
Star Strider el 25 de Sept. de 2021
MathWorks reply —
Thank you for submitting this report. I understand that in the example provided, the "smoothdata" function works better when used with individual columns compared to using it with the complete matrix as input.
  1. Reason for the behavior:
This behavior is due to the way the window size argument is computed. When the window size for the smoothing method is not specified, smoothdata computes a default window size based on a heuristic. This algorithm works better for individual vectors.
In the example provided, the window size differs between the 2 use-cases:
  1. When using the complete matrix as an input. The computed window size is 1.
  2. When using a loop to apply the smoothdata function on each column. The window size is computed for each iteration of the loop. If we store each new window size in a vector, we would observe that each column outputs a different window size. Below is an example on how to do so: [B, matrixWindowSize] = smoothdata(a);
matrixWindowSize % output=1
columnWindowSizes = [];
for k = 1:size(a,2)
[a_smoothed(:,k),columnWindowSizes(end+1)] = smoothdata(a(:,k));
end
columnWindowSizes % output=[2 2 1 2 1 2 2 1 3
% 2 8 1 4 1 3 2 7 6 1 1 1 1]
  1. Potential workaround:
One potential workaround to this issue is to specify the window size in the arguments of the “smoothdata” function as shown in the line below:
windowSize = 4;
smoothdata(a,'movmean',windowSize);
Note: window size needs to be the 3rd arguments after the method used. In the example above I am using ‘movmean’ which is the default method.
Note that using the function ”smoothdata” on each individual column might still work better as it will compute a corresponding window size for each column.
I will submit a bug report about the smoothdata function to the development team and that might be added in a future update.
I will now close this case. If you have any additional questions, please feel free to reply to this email and I will be happy to re-open the case to assist you further.
Thanks,
Tahar
I have nothing further to add.
.

Iniciar sesión para comentar.


Jan
Jan el 23 de Sept. de 2021
It works fine in the current online version:
a = rand(20, 22) + (0:21);
b = smoothdata(a);
plot(a, '--')
hold('on');
plot(b)

Categorías

Más información sobre Data Preprocessing en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by