How can I fix this error?

1 visualización (últimos 30 días)
Hazal Yuvgun
Hazal Yuvgun el 7 de Abr. de 2021
Comentada: Hazal Yuvgun el 7 de Abr. de 2021
  4 comentarios
Hazal Yuvgun
Hazal Yuvgun el 7 de Abr. de 2021
i is row
Hazal Yuvgun
Hazal Yuvgun el 7 de Abr. de 2021
I want to extract the first row from others

Iniciar sesión para comentar.

Respuesta aceptada

Khalid Mahmood
Khalid Mahmood el 7 de Abr. de 2021
Editada: Khalid Mahmood el 7 de Abr. de 2021
function y=Question5(x)
y=[];
rows=size(x,1);
y(1,:)=x(1,:);
for i=2:rows
y(i,:)=x(i,:)-x(1,:);
end
end
  3 comentarios
Khalid Mahmood
Khalid Mahmood el 7 de Abr. de 2021
Editada: Khalid Mahmood el 7 de Abr. de 2021
in for loop y(i) ensures all rows. Like y(i,:)=x(i,:)-x(1,:); This is working now
Hazal Yuvgun
Hazal Yuvgun el 7 de Abr. de 2021
Thank you so much

Iniciar sesión para comentar.

Más respuestas (2)

Steven Lord
Steven Lord el 7 de Abr. de 2021
Where in your code do you define a variable named i? If the answer is some form of "nowhere" or "I don't" then MATLAB will call the built-in function to determine the value that should be used. That built-in function returns neither a positive integer value nor a logical value.

Khalid Mahmood
Khalid Mahmood el 7 de Abr. de 2021
function y=Question5(x,i)
y=[];
if i==1
y=x(1,:);
else
y=x(i,:)-x(1,:);
end
end
  1 comentario
Khalid Mahmood
Khalid Mahmood el 7 de Abr. de 2021
save above code as Question5.m file. Then
>>x=randi([-10 10],5,3); % define x, or whatever x you want
>>y=Question5(x,1)
>>y=Question5(x,2)
>>y=Question5(x,3)
But I think you may need following code for advanced logic.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by