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 个评论

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.
I am grateful for your concern and help.
To help you attend the error in the code, I have attached the code and the data used.
The one matrix data is one sample of our data and works well. The total matrix data is the data of the 231 samples which results this error.
I hope you can run both data and comprehend the error better.
Thank you
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.
And use the debugger to step through your code and see what it's actually doing.
I sincerely appreciate your comment. Indeed what you said is true. The root problem has to do with the dimensions associated with the code.
Attached is the original code. I only tried to changed the dimensions to work with the matrices that is why I tried this
if size(x, 1) > 1
row_number = size(x, 1);
end
For the original code, the error is :
Error using horzcat Dimensions of matrices being concatenated are not consistent.
I wil be grateful if you can help change the dimensions to work with matrices and the above data.
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.
I am sincerely grateful for your concern and help.
M =x;
%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
After using this the error below occured:
tvf_emd(x)
Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding your available stack
space can crash MATLAB and/or your computer.
Error in tvf_emd
I dont know if I did the right loop as you suggested and also the defining of M.
The loop must be in your code that calls tvf_emd not in tvf_emd itself.
please I do not understand exactly what the above statement means. Please in that case will my input value be the same as M ?
Please I wish you can help further?
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!

Translated by