Subscripted assignment dimension mismatch error

am working on an image processing project and i am getting an error on line 35. the error is Subscripted assignment dimension mismatch error. i have tried to fix it in vain. could someone help me please
  1. %D0=25x25 image(double)
  2. %Y=25x5000 signal(double)
  3. [m,n0] = size(D0);
  4. [~,N] = size(Y);
  5. itcmat = zeros(iters/iter_check, n0-nmin+1);
  6. D0run = D0;
  7. ii = 1;
  8. vn = [n0]; % vector of actual sizes
  9. for it = 1 : iter_check : iters
  10. % run the algorithm for a few iterations
  11. [D, X, ~] = DL(Y, D0run, s, iter_check, str2func(update), params, 'replatom', replatom);
  12. % order dictionary based on representation power
  13. [~, atom_list] = sort(sum(X.*X,2), 'descend');
  14. % compute ITC for all dictionary sizes (with ordered atoms)
  15. n1 = max(nmin, vn(ii) - ncand);
  16. for n2 = n1 : vn(ii)
  17. Dn = D(:, atom_list(1:n2));
  18. Xn = omp(Y, Dn, s);
  19. rmse = norm(Y-Dn*Xn, 'fro')/sqrt(m*N);
  20. if itc_index > 10
  21. itc2 = com_itc_BS_2( Y, Dn, Xn, rmse );
  22. itcmat(ii,n2-n1+1) = itc2(itc_index-10);
  23. else
  24. [~, itc2] = com_itc_BS( Y, Dn, Xn, rmse );
  25. itcmat(ii,n2-n1+1) = itc2(itc_index);
  26. end
  27. end
  28. % trim or enlarge dictionary
  29. [~,n2] = min(itcmat(ii,1:vn(ii)-n1+1)); % compute min value of ITC
  30. n2 = n2 + n1 - 1;
  31. nopt(ii) = n2; %% error Subscripted assignment dimension mismatch.
  32. itcmat(ii,vn(ii)-n1+2:n0-n1+1) = itcmat(ii,vn(ii)-n1+1); % fill with last value of ITC
  33. if n2 < vn(ii) - nminus % dictionary is too big
  34. n2 = max(nmin, vn(ii) - nminus);
  35. D0run = D(:, atom_list(1:n2)); % continue with new dictionary
  36. elseif n2 < vn(ii)-1
  37. n2 = max(nmin, vn(ii) - 1);
  38. D0run = D(:, atom_list(1:n2)); % continue with new dictionary
  39. elseif n2 == vn(ii) % dictionay may be too small, increase size
  40. %n = vn(ii);
  41. n2 = vn(ii) + nplus;
  42. Dplus = randn(m,nplus);
  43. Dplus = normc(Dplus);
  44. D0run = [D Dplus]; % continue with new dictionary
  45. end
  46. vn(ii+1) = n2;
  47. % final operations
  48. X = omp(Y, D0run, s); % compute RMSE just to see it
  49. rmse = norm(Y-D0run*X, 'fro')/sqrt(m*N);
  50. rmsev(ii) = rmse;
  51. ii = ii+1;
  52. end
  53. % final refinement: a few DL iterations with the final size
  54. if nopt(ii-1) < vn(ii-1)
  55. D0run = D(:, atom_list(1:nopt(ii-1))); % cut dictionary to optimal number of atoms
  56. end
  57. [D, ~, ~] = DL(Y, D0run, s, iter_check, str2func(update), params, 'replatom', replatom);
  58. X = omp(Y, D, s);
  59. rmse = norm(Y-D*X, 'fro')/sqrt(m*N);
  60. % final operations
  61. X = omp(Y, D0run, s); % compute RMSE just to see it
  62. rmse = norm(Y-D0run*X, 'fro')/sqrt(m*N);
  63. rmsev(ii) = rmse;
  64. ii = ii+1;
  65. end
  66. % final refinement: a few DL iterations with the final size
  67. if nopt(ii-1) < vn(ii-1)
  68. D0run = D(:, atom_list(1:nopt(ii-1))); % cut dictionary to optimal number of atoms
  69. end
  70. [D, ~, ~] = DL(Y, D0run, s, iter_check, str2func(update), params, 'replatom', replatom);
  71. X = omp(Y, D, s);
  72. rmse = norm(Y-D*X, 'fro')/sqrt(m*N);

2 Kommentare

Adam
Adam am 2 Okt. 2019
Bearbeitet: Adam am 2 Okt. 2019
You should be able to determine what the problem is in 10s using the debugger and the 'Pause on errors' option. It is a lot more difficult to just scan 85 lines of code and try to work out what is happening.
nopt is not predeclared, which may be the cause, but equally if n2 is non-scalar on any iteration of the loop then this would give this error, as it would also if n2 is empty ( [] ).
Guillaume
Guillaume am 2 Okt. 2019
Bearbeitet: Guillaume am 2 Okt. 2019
In my opinion, it would be much easier to read the code if it had been formatted as code (with no line numbers) using the button.
The error is because ii and n2 are not the same size. As Adam says, the debugger is the best way to find out what size they are.
We have
[~,n2] = min(itcmat(ii,1:vn(ii)-n1+1)); % compute min value of ITC
n2 = n2 + n1 - 1;
nopt(ii) = n2; %% error Subscripted assignment dimension mismatch.
So, n2 (at least this n2 since n2 is used for many purposes including the loop index. An infinity of variable names available, yet n2 has to be reused many times!) is the index of the minimum of a row of a matrix. So it must be scalar.
Most likely n1 is not scalar. n1 is the max of two things which do not appear anywhere else in the code, so we don't know what their size is. Most likely, nmin or ncand is not scalar.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Gefragt:

am 2 Okt. 2019

Bearbeitet:

am 2 Okt. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by