조회 수: 2 (최근 30일)
Ahsan Khan 2014년 12월 18일
답변: Julia 2014년 12월 18일
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 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

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

### 카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by