MATLAB Answers

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

조회 수: 18(최근 30일)
BobbyRoberts
BobbyRoberts 2021년 9월 23일
댓글: Star Strider 2021년 9월 25일 16:24
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!

답변(2개)

Star Strider
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
Star Strider 2021년 9월 25일 16:24
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.
.

댓글을 달려면 로그인하십시오.


Jan
Jan 2021년 9월 23일 13:33
It works fine in the current online version:
a = rand(20, 22) + (0:21);
b = smoothdata(a);
plot(a, '--')
hold('on');
plot(b)
  댓글 수: 1
BobbyRoberts
BobbyRoberts 2021년 9월 23일 15:21
Weird... it works with the fake rand-generated data, but not with my matrix.

댓글을 달려면 로그인하십시오.

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by