Error using horzcat Dimensions of arrays being concatenated are not consistent
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Sunshine
el 21 de Mzo. de 2024
Comentada: Sunshine
el 23 de Mzo. de 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 comentario
Walter Roberson
el 21 de Mzo. de 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)];
Respuesta aceptada
Walter Roberson
el 21 de Mzo. de 2024
Editada: Walter Roberson
el 21 de Mzo. de 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]
Más respuestas (0)
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!