for loop iteration keeps previous vector length....why? please help

3 次查看(过去 30 天)
Hi there guys,
I have written a simple code (shown at the bottom). the problem is that when the loop starts and goes to the next iteration the vector size of the previous calculation is stored...why? every time a new iteration starts isn't suppose to start fresh. for debugging purposes I see the lengths of vector each iteration makes and to my surprise the vector size from the previous iteration is kept. how do I make this loop so at each iteration, its a fresh start of the variables. please help. the output i get is shown below along with how its suppose to come out
len = 4 >>this is correct
len2 = 4 >>this is correct
len = 0 >>this is correct
len2 = 4 >>this should be 0
len = 1 >>this is correct
len2 = 4 >>this should be 1
len = 0 >>this is correct
len2 = 4 >>this should be 0
len = 2 >>this is correct
len2 = 4 >>this should be 2
if true
data = [1,3,5,7,1,5,1,7,8,1];
for ii = 1:5
indxe = find(data' == ii); %find value
%to find the row in which the value resides in
for j = 1:length(indxe)
indx_row(j,:) = floor(((indxe(j)-1)/8)+1); %each row increment is 0.125
end
len = length(indxe)
len2 = length(indx_row)
end
end

采纳的回答

Julia
Julia 2014-12-18
Hi,
your problem is that indx_row has its greates length in the first loop iteration. This length is stored. After the first loop iteration your program keeps the length of indx_row and overwrites only the entries in indx_row.
data = [1,3,5,7,1,5,1,7,8,1];
for ii = 1:5
indxe = find(data' == ii); %find value
indx_row = [];
%to find the row in which the value resides in
for j = 1:length(indxe)
indx_row(j,:) = floor(((indxe(j)-1)/8)+1); %each row increment is 0.125
end
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Measurements and Statistics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by