how to solve this error "Subscripted assignment dimension mismatch."
显示 更早的评论
I tried looping through 231 samples of data but the error below occurs,
Subscripted assignment dimension mismatch.
Error in tvf_emd (line 132)
imf(nimf,:)=y(ind_remov_pad);
if flag_stopiter
for row_number = 1 : size(imf,1);
end
imf(nimf,:)=y(ind_remov_pad);
temp_x=temp_x - y(ind_remov_pad);
break;
each Imf is supposed to be 50x 500 but due to the 231 data samples , the imf matrix multiplies to become 50 x 115500.
This causes the above error even though I tried looping through each sample.
Please I will be grateful for any help.
9 个评论
Guillaume
2019-10-12
All we can say is that the number of columns in imf is different from the numbers of indices in ind_remov_pad or the number of 1s if ind_remov_pad if it's a logical vector. Of course, if you try to put a number of values into a row with a different number of elements, matlab will error.
As your code doesn't show where any of these come from and doesn't have any comment, we can't say if it's due to a bug or simply bad coding.
Yussif M. Awelisah
2019-10-12
Guillaume
2019-10-12
I'm sorry to say that your code is a mess. First, fix the indentation (ctrl+I in the editor) and look at all the orange lines on the right of the editor that tells you of potential problems. Then, there's the lack of comments that explain the overall aim of the code.
But more important, there's all the stuff that doesn't make sense and the bugs. e.g:
if size(x,1) > 1 % convert to row vector
for row_number = 1 : size(x,1)
end
end
That comment is misleading, we have a loop that does nothing, it certain doesn't do any conversion to row vector. You may be tempted to remove that loop (and you should) but a side effect of the loop is that it creates the variable row_number and set it to the numbers of rows of the input, so really that code is equivalent to:
if size(x, 1) > 1
row_number = size(x, 1);
end
It's important because row_number is used later on. And here is the first problem, if x is a row vector, row_number never gets created and your code will error with an 'undefined variable or function row_number' error. This is indeed the case with your 'one_matrix_data'. The whole logic of these four lines is completely broken.
There are many more problems (e.g. if your code were to succeed, on the first iteration row_number would be size(x, 1), on all the other iterations row_number would be MAX_IMF as a side effect of another do nothing loop) but the main issue is that your code appears to be designed to work with row vectors only but has bugs like the above that prevent it to do so.
One thing for sure, your code was never designed to work with matrices and yes you'll see the error you had if you pass it a matrix.
So you need to go over everything you've written and think about what your inputs are. Are they row vectors, column vectors or matrices. You can't use numel or length with matrices.
Yussif M. Awelisah
2019-10-14
Guillaume
2019-10-14
The original code is designed to work with vectors only. Due to the lack of useful comments I've no idea what it's meant to do (and I'm not prepared to spend an hour trying to understand it) so I'm not sure if what it's doing is even meaningful for a matrix. In any case, adapting it to matrices would require a near complete rewrite.
If you want to use that code on each row of a matrix, simply call that function in a loop:
%M: the matrix you want to use with tvf_emd.
out = zeros(50, size(M, 2), size(M, 1)); %output of tvf_emd is 2D, so create a 3D matrix to store the output for each row
%Note that the number of rows of the output of tvf_emd is hardcoded to 50 (!!) If that were to change, the above needs to change accordingly.
for row = 1:size(M, 1)
out(:, :, row) = tvf_emd(M(row, :));
end
Then, out(:, :, i) is the output of tvf_emd for row i of M.
Yussif M. Awelisah
2019-10-15
Guillaume
2019-10-15
The loop must be in your code that calls tvf_emd not in tvf_emd itself.
Yussif M. Awelisah
2019-10-15
Guillaume
2019-10-15
tvf_emd is a function, therefore you must be calling it from some other code (the same code that actually imports your data), maybe it's a script. The loop is in that other code. The function itself should be unmodified, e.g.
%script or function, NOT tvf_emd.m
matcontent = load('Total matrix data..mat')
DS_DATA_ODD = matcontent.DS_DATA_ODD;
out = zeros(50, size(DS_DATA_ODD, 2), size(DS_DATA_ODD, 1))
for row = 1:size(DS_DATA_ODD, 1)
out(:, :, row) = tvf_emd(DS_DATA_ODD(row, :));
end
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!