Error using horzcat Dimensions of arrays being concatenated are not consistent
4 views (last 30 days)
Show older comments
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(:)];
%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 Comment
Walter Roberson
on 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);
%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)];
Accepted Answer
Walter Roberson
on 21 Mar 2024
Edited: Walter Roberson
on 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);
%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
col_stu_max = [group temp]
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!