smoothdata function not working for matrix . any one have ideas to what is going on?
9 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(2 个)
Star Strider
2021-9-23
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 个评论
Star Strider
2021-9-23
编辑:Star Strider
2021-9-23
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/747664/image.png)
.
Star Strider
2021-9-25
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.
- 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:
- When using the complete matrix as an input. The computed window size is 1.
- 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]
- 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.
.
Jan
2021-9-23
It works fine in the current online version:
a = rand(20, 22) + (0:21);
b = smoothdata(a);
plot(a, '--')
hold('on');
plot(b)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!