what's wrong with the code

for i=1:n
for j=1:n
if (5<i<15 & 8<j<12 & j=i) {d(i,j)=1};
else d(i,j)=0;
end
end
end

Respuestas (3)

Matt J
Matt J el 25 de Mzo. de 2013
Editada: Matt J el 25 de Mzo. de 2013

1 voto

Of course, the whole thing could be done much more simply and without loops,
z=zeros(1,n);
z(min(9:11,n))=1;
d=diag(z);
Azzi Abdelmalek
Azzi Abdelmalek el 25 de Mzo. de 2013

0 votos

n=40
for i=1:n
for j=1:n
if (5<i & i<15 & 8<j & j<12 & j==i) d(i,j)=1;
else d(i,j)=0;
end
end
end
Youssef  Khmou
Youssef Khmou el 25 de Mzo. de 2013
Editada: Youssef Khmou el 25 de Mzo. de 2013

0 votos

modify,
for i=1:n
for j=1:n
if (5<i<15 && 8<j<12 && j==i)
d(i,j)=1;
else d(i,j)=0;
end
end
end

1 comentario

Walter Roberson
Walter Roberson el 25 de Mzo. de 2013
This has the same bug as the original. 5<i<15 means ((5<i)<15) which means "(true (1) or false (0)) < 15" which is always true.

La pregunta está cerrada.

Etiquetas

Preguntada:

el 25 de Mzo. de 2013

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by