"the variable assigned to a may be unused"
11 ビュー (過去 30 日間)
古いコメントを表示
Mattlab just returns the name of my function when i run it and the warning message that comes up currently says "the variable assigned to a may be unused" but im not sure what im doing wrong.
%the heap sort fuction
function sorted = heapsort(x)
%first we must build initial max-heap from x
%we need to find the length of the array
n = length(x);
%and create an initial max-heap with the given parameters of (array,
%length)
maxheap = buildmaxheap(x,n);
%initialise the array for the sorted elements
sorted = zeros(1,n);
% now the heapsort algorithm is as follows
for i = 1:n
k = n+1-i; %to find largest i in array
%put (n+1-i)th largest element in place
x = swap(x,1,k);
%adding final value to the sorted array
sorted(k) = maxheap(k);
%removing final value
maxheap(k) =[];
%create the next maxheap
n = n - 1;
maxheap = buildmaxheap(maxheap,i,n);
end
end
%the build max heap function
function maxheap = buildmaxheap(x,n)
a = x;
for i = floor(n/2):-1:1
%call heapify to make it sorted
a = maxheapify(a,i,n);
end
maxheap = a ;
end
%heapify function
function a = maxheapify(x,i,n)
%calculating the indicies of the child nodes
lhn = 2*i;
rhn = 2*i+1;
a = x;
%comparing left hand child node
if ((lhn <= n) && (a(lhn) > a(i))) %l<=n to check its not a leaf and a(l)>a(i) to find the biggest of the two
biggest = lhn;
else
biggest = i; %setting biggest found value to compare with rhn
end
%comparing right hand child node
if ((rhn <= n) && (a(rhn) > a(biggest))) %comparing rhn with biggest found previously
biggest = rhn;
end
%checking if it is complete
if biggest ~= i
%swap
a = swap(a,i,biggest);
%recursively call itself if its not finished
a = maxheapify(a,biggest,n);
end
a =x ;
end
function x = swap(x,i,j)
%swap elements i and j in vector(or this case array) x
val = x(i);
x(i) = x(j);
x(j) = val;
end
0 件のコメント
回答 (2 件)
dpb
2020 年 1 月 5 日
In
%the build max heap function
function maxheap = buildmaxheap(x,n)
a = x;
for i = floor(n/2):-1:1
%call heapify to make it sorted
a = maxheapify(a,i,n);
end
maxheap = a ;
end
the x isn't used after the assign...I currently have lost access at least temporarily so can't test; not sure if that's what's generating the warning or not; you forgot to put the error message in full context of the calling code and the traceback so have to guess precisely where it was triggered.
Image Analyst
2020 年 1 月 5 日
In this code:
if biggest ~= i
%swap
a = swap(a,i,biggest);
%recursively call itself if its not finished
a = maxheapify(a,biggest,n);
end
a =x ;
let's assume that the if criteria passes and you then execute an assignment to a:
a = maxheapify(a,biggest,n);
Well, as soon as that's finished, you go and assign x to a, overwriting the a you just created:
a =x ;
So the first assignment of the output of maxheapify() to a never gets used. It's assigned but immediately overwritten. So the warning (it's not an error) just notifies you of that situation in case that's not what you wanted.
2 件のコメント
Image Analyst
2020 年 1 月 5 日
What did you pass in to heapsort() for x? You didn't just click the green run triangle without giving anything for x did you?
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!