Problem 32. Most nonzero elements in row

3 visualizaciones (últimos 30 días)
ricardo gunawan
ricardo gunawan el 15 de Jul. de 2013
function r = fullest_row(a)
x=sum(a==0);
y=max(x);
row=0;
for i=1:length(x)
if x(i)==y
row=i;
end
r=row;
end
whats wrong with the code???
  2 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 15 de Jul. de 2013
Editada: Azzi Abdelmalek el 15 de Jul. de 2013
Give a short example: a= ... and what should be the result?
Jan
Jan el 15 de Jul. de 2013
@ricardo: Please do not let us solve your Cody problems. This is not the intention of this game and of this forum.

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 15 de Jul. de 2013
Editada: Matt J el 15 de Jul. de 2013
Probably this line
x=sum(a==0);
should really be this
x=sum(any(a),2);
The whole thing looks like it could be done in 1 line
[~,row]=max( sum(any(a),2) );
  1 comentario
ricardo gunawan
ricardo gunawan el 15 de Jul. de 2013
thx that help alot anyway the code i modified a little
[~,row]=max( sum(a~=0,2) );
where do you learn and pratice matlab? i want to learn it fully

Iniciar sesión para comentar.

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 15 de Jul. de 2013
I don't know what should be the result r, but I can see that the value of r is erased each loop
r=rows
maybe you can initialize r
r=[]
then write
r(end+1)=rows

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by