# "the variable assigned to a may be unused"

8 views (last 30 days)
Ghazal Ramzan ali on 5 Jan 2020
Commented: Image Analyst on 5 Jan 2020
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

dpb on 5 Jan 2020
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 Comment

Ghazal Ramzan ali on 5 Jan 2020
im not sure what you mean, how would i resolve this?

Image Analyst on 5 Jan 2020
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.