Why do I get a matrix dimensions must agree error in PCT on external server but not on local cluster?

4 views (last 30 days)
Garima Sharma
Garima Sharma on 18 Jun 2017
Commented: Mostafa Nakhaei on 21 Feb 2020
Hello,
When I run some code in matlab using parallel computing on my local cluster (2 workers) it works perfectly. However, when I run the same code on an external cluster (20 workers) I get a "matrix dimensions must agree" error for the last line of this chunk of code. I can't figure out why! Any suggestions appreciated.
parfor it = 1:num_noise_iter
y_i = normrnd(y_no_noise,outcome_stdev);
z=[group_id, y_i, y_no_noise, preferences, effort_vector, u1];
z_sorted=sortrows(z, [1,-2]);
y_i_gp = accumarray(z_sorted(:,1),z_sorted(:,2),[],@(n){n});
y_i_gp_mat=padcat(cell2mat(y_i_gp(1,:)), cell2mat(y_i_gp(2,:)))';
Ninsp = max(cellfun(@numel,y_i_gp));
y_i_g = y_i_gp_mat(z_sorted(:,1),:);
y_out=y_i_g';
% error in following line: "Matrix dimensions must agree"
y_out=reshape(y_out(y_out ~= z_sorted(:,2)'),[],num_inspectors);
  1 Comment
Edric Ellis
Edric Ellis on 20 Jun 2017
Without a minimal, complete, verifiable example, it's hard to tell what's going wrong. You could try and work things out by splitting the combined expression over several lines, and using disp to print the sizes of the various expressions.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 20 Jun 2017
Are you running R2016b or newer on your local system, and is the external cluster running R2016a or earlier? If so then you might be benefiting from the new implicit expansion feature of R2016b that operates like bsxfun()
  3 Comments
Walter Roberson
Walter Roberson on 20 Jun 2017
Ah, you are comparing an 11 x 21 matrix to a 1 x 21 matrix. With R2016b or later, you would get an 11 x 21 logical result.
At the moment I do not follow the math of how z_sorted and y_out are connected, so at the moment I do not see any reason why the number of matches per row should be consistent. You can theoretically get duplicate random numbers, or random numbers that just happen to match integers. It is after 3 AM here so I although I have traced through the construction logic some, I am not currently convinced that you will get a fixed number of rows or columns selected out of the comparison.
If you do not get a fixed number of matches per row then the reshape() is not going to work because the number of total elements would not match the number of inspectors. If we suppose that the fixed number of per row might not be exactly 1, then we run into the logic difficulty that you would be reshaping 11 x something into [] by 11 without having done a transpose, and that is unlikely to get you the order of elements that you want. If we somehow determine that there will be exactly one match per row, so the number of elements selected matches the number of inspectors, then the reshape() in that form would have the effect of making the result into a row vector -- but there are clearer ways of making that into a row vector. For example, reshape(Expression, 1, []) makes it clear that your desired result is a row vector. Or, considering that the result of the logical masking is going to be a column vector, you could just use .' instead of reshape() in the way you do.
Anyhow, the direct repair to your code to do what you are doing now is
reshape(y_out( bsxfun(@ne, y_out, z_sorted(:,2)')),[], num_inspectors)

Sign in to comment.


Mostafa Nakhaei
Mostafa Nakhaei on 21 Feb 2020
I have the same issue and I only access to Matlab 2016a
What should I do?
  2 Comments
Mostafa Nakhaei
Mostafa Nakhaei on 21 Feb 2020
Thanks Walter, I fixed the problem with a bit of mathematical trick to change the dimension and preserve the original results.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by