Error using horzcat Dimensions of arrays being concatenated are not consistent
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Sunshine
il 21 Mar 2024
Commentato: Sunshine
il 23 Mar 2024
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 Commento
Walter Roberson
il 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)];
Risposta accettata
Walter Roberson
il 21 Mar 2024
Modificato: Walter Roberson
il 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]
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!