Error using horzcat Dimensions of arrays being concatenated are not consistent

4 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2024-3-21
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.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2024-3-21
编辑:Walter Roberson 2024-3-21
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by