"the variable assigned to a may be unused"

조회 수: 13 (최근 30일)
Ghazal  Ramzan ali
Ghazal Ramzan ali 2020년 1월 5일
댓글: Image Analyst 2020년 1월 5일
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

답변 (2개)

dpb
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.
  댓글 수: 1
Ghazal  Ramzan ali
Ghazal Ramzan ali 2020년 1월 5일
im not sure what you mean, how would i resolve this?

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


Image Analyst
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
Ghazal  Ramzan ali
Ghazal Ramzan ali 2020년 1월 5일
Thank you so much, the code no longer has any error warnings however it still wont run and is saying there is an error on line 6 with n = length(x)?
Image Analyst
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 CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by