Error using horzcat Dimensions of arrays being concatenated are not consistent

1 vue (au cours des 30 derniers jours)
My goal is to display the distinct u_id along with the highest score. The u_id can have multiple entries.
1) I create NX2 double array of the u_id and score
2) Get rid of 1st row since it's not an actual score submission
3) Remove all NaN references
4) Create array of unique u_ids
5) Return max score for u_id (This is where I error. Everything works when I step through the code til this point.) Error: Error using horzcat
Dimensions of arrays being concatenated are not consistent.
%{
Followed this code as reference (which works)
A_test = [1 12; 1 16; 2 5; 2 13; 3 9; 3 19; 3 50];
Au_test = unique(A_test(:,1));
B_test = [Au_test accumarray(A_test(:,1), A_test(:,2), [], @max)]
%}
col_test = [(double(test.u_id(:))), test.score(:)];
Unable to resolve the name 'test.u_id'.
%remove 1st entry
col_test_update = col_test(2:end, :);
%Remove all NaN
Au_nonNaN = col_test_update(~any(isnan(col_test_update),2),:) ;
%get unique user_id values and put in array
Au = unique(col_test_update(:,1));
%display Au array
disp(Au);
%return user_ids and max scores
%B_test = [Au_test accumarray(A_test(:,1), A_test(:,2), [], @max)];
col_stu_max = [Au accumarray(Au_nonNaN(:,1), Au_nonNaN(:,2), [], @max)];
  1 commentaire
Walter Roberson
Walter Roberson le 21 Mar 2024
test = readtable('test.csv');
col_test = [(double(test.u_id(:))), test.score(:)];
%remove 1st entry
col_test_update = col_test(2:end, :);
%Remove all NaN
Au_nonNaN = col_test_update(~any(isnan(col_test_update),2),:) ;
%get unique user_id values and put in array
Au = unique(col_test_update(:,1));
%display Au array
disp(Au);
226340 1038935 1210883 1241801 1354063 1387758 1394342 1568429 1570710 1580013 1584550 1722950 1761525 1772379 1779020
%return user_ids and max scores
%B_test = [Au_test accumarray(A_test(:,1), A_test(:,2), [], @max)];
col_stu_max = [Au accumarray(Au_nonNaN(:,1), Au_nonNaN(:,2), [], @max)];
Error using horzcat
Dimensions of arrays being concatenated are not consistent.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 21 Mar 2024
Modifié(e) : Walter Roberson le 21 Mar 2024
test = readtable('test.csv');
col_test = [(double(test.u_id(:))), test.score(:)];
%remove 1st entry
col_test_update = col_test(2:end, :);
%Remove all NaN
Au_nonNaN = col_test_update(~any(isnan(col_test_update),2),:) ;
[G, group] = findgroups(Au_nonNaN(:,1));
%get unique user_id values and put in array
Au = unique(col_test_update(:,1));
%display Au array
disp(Au);
226340 1038935 1210883 1241801 1354063 1387758 1394342 1568429 1570710 1580013 1584550 1722950 1761525 1772379 1779020
%return user_ids and max scores
%B_test = [Au_test accumarray(A_test(:,1), A_test(:,2), [], @max)];
temp = accumarray(G, Au_nonNaN(:,2), [], @max);
whos Au temp
Name Size Bytes Class Attributes Au 15x1 120 double temp 15x1 120 double
col_stu_max = [group temp]
col_stu_max = 15x2
226340 5 1038935 5 1210883 5 1241801 5 1354063 5 1387758 0 1394342 5 1568429 5 1570710 5 1580013 5

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by