I am asking this question after quite some time since I have used Matab.
This is given mysort.m
and i am trying to drive to
but i am getting error on line4
can some one explain why am i getting error and, how to debug them?

댓글 수: 2

Giha
Giha 2024년 1월 7일
This is the code!
function y = mysort(x)
% Emulates the Matlab internal function ‘sort’
N = length(x);
for j = N:-1:2
track = x(1);
tracki = 1;
for i = 2:j
if x(i) > track
track = x(i);
tracki = i;
end
end
x(tracki) = x(j);
x(j) = track;
end
y = x;
Giha
Giha 2024년 1월 7일
The function is applied in Matlab as shown:
>> y = [-10 8 -4 3 12 -15];
>> x = mysort(y);

댓글을 달려면 로그인하십시오.

 채택된 답변

Dyuman Joshi
Dyuman Joshi 2024년 1월 7일
편집: Dyuman Joshi 2024년 1월 7일

1 개 추천

Remove the clear all, close all, clc line.
You generally don't need to use those commands, specially not inside a function.
Note that the file mysort() is not a script, it's a function.
Also, while asking a question here, you should share your code (by copy-pasting or sharing the file) instead of posting a picture of it.

댓글 수: 9

Giha
Giha 2024년 1월 7일
Joshi!
I tried to removed them and got same error!
Giha
Giha 2024년 1월 7일
Also Thank you for letting me know!
Dyuman Joshi
Dyuman Joshi 2024년 1월 7일
편집: Dyuman Joshi 2024년 1월 7일
@Giha, The code seems to be working here, without that line -
x = [-10 8 -4 3 12 -15];
y = mysort(x)
y = 1×6
-15 -10 -4 3 8 12
function y = mysort(x)
% Emulates the Matlab internal function ‘sort’
N = length(x);
for j = N:-1:2
track = x(1);
tracki = 1;
for i = 2:j
if x(i) > track
track = x(i);
tracki = i;
end
end
x(tracki) = x(j);
x(j) = track;
end
y = x;
end
Giha
Giha 2024년 1월 7일
@Dyuman Joshi I must be doing something wrong
I am still getting
this error
Error in mysort (line 4)
N = length(x);
Thank you for helping me out,
I am trying to learn matlab this year
Dyuman Joshi
Dyuman Joshi 2024년 1월 7일
Please share the file mysort(). Use the paperclip button to attach.
Giha
Giha 2024년 1월 7일
Here is the attachment!
It works here, without any errors.
x = [-10 8 -4 3 12 -15];
y = mysort(x)
y = 1×6
-15 -10 -4 3 8 12
If you get an error while running the file, I suspect there might be a duplicate file named mysort(). Please share the output of this code -
which mysort -all
Giha
Giha 2024년 1월 7일
@Dyuman Joshi Thank you for helping! i got it to work with your comment!
Dyuman Joshi
Dyuman Joshi 2024년 1월 7일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 White에 대해 자세히 알아보기

제품

릴리스

R2023b

태그

질문:

2024년 1월 7일

댓글:

2024년 1월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by