How can I fix this error?

4 Comments

Your question is not clear. What is i?
%Supposed this
function y=Question5(x,i)
.....
end
Let, x=randi([-10 10],5,3)
y=Question5(x,3)
i is row
I want to extract the first row from others

Sign in to comment.

 Accepted Answer

Khalid Mahmood
Khalid Mahmood on 7 Apr 2021
Edited: Khalid Mahmood on 7 Apr 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 Comments

Output is not correct. Output only shows the last row
Khalid Mahmood
Khalid Mahmood on 7 Apr 2021
Edited: Khalid Mahmood on 7 Apr 2021
in for loop y(i) ensures all rows. Like y(i,:)=x(i,:)-x(1,:); This is working now
Thank you so much

Sign in to comment.

More Answers (2)

Steven Lord
Steven Lord on 7 Apr 2021

0 votes

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.
function y=Question5(x,i)
y=[];
if i==1
y=x(1,:);
else
y=x(i,:)-x(1,:);
end
end

1 Comment

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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!