필터 지우기
필터 지우기

Problems with for-loop.

조회 수: 1 (최근 30일)
Max
Max 2016년 2월 23일
댓글: dpb 2016년 2월 27일
Hello I would like to create a for-loop with a few conditions like that:
tfail_or_ges is a 1x40000 vector consists of random numbers.
z=zeros(1,length(t));
for k=1:length(tfail_or_ges);
if t(k)==tfail_or_ges_sort(k)
z(k)=0;
else
z(k)=1;
end
end
Now, the problem is that if tfail_or_ges(k=40001) is empty I get the value z(k=40001)=0. And that´s wrong.
Can somebody help me, please?
  댓글 수: 1
Torsten
Torsten 2016년 2월 23일
But your for-loop is a loop from k=1 to k=40000. Why does it address tfail_or_ges(40001) ?
Best wishes
Torsten.

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

답변 (1개)

dpb
dpb 2016년 2월 23일
편집: dpb 2016년 2월 23일
The loop above in Matlab parlance is simply
z(1:length(tfail_or_ges))=(t==tfail_or_ges_sort);
where z will be the logical vector of 0,1 for false,true. Your z will be a double instead of logical but that probably will be of no consequence but if is important simply convert.
As for the actual question/concern, that's also just "how Matlab works". A numeric or logical array cannot hold "empty"; every element must be of the same class and be a valid element of such class (including NaN or +/-Inf as possible values for floating point). "Empty" for those classes refers only to the array itself, not to individual elements within. A cell array, otoh, may have an empty cell within the array of cells irrespective of other cells.
Hence, one is left to draw the conclusion that
length(tfail_or_ges)<length(tfail_or_ges_sort)
and t was initialized based on the longer length. If you want to be able to find those extra locations uniquely, use
t=nan(length(tfail_or_ges_sort),1);
to preallocate rather than zeros or simply use the vector length of the shorter and as noted above dispense with the loop entirely excepting create the shorter version--
z=(t(1:length(tfail_or_ges)))==tfail_or_ges_sort);
  댓글 수: 2
Max
Max 2016년 2월 27일
편집: dpb 2016년 2월 27일
Hello dpb, thank you for your answer. First of all, I don´t really understand where I shall to put the code above in my for loop.
And second:
Can I also do it like that:
for k=1:length(t)-1;
z=1.*(t>=0 & t<tfail_or_ges_sort(k));
z(k,:)=z+0.*(t>=tfail_or_ges_sort(k));
end
dpb
dpb 2016년 2월 27일
You don't need any loop at all...create a short subset of (say) 10 elements of the vector at the command line so you can observe the results easily without the distraction of very long vectors. Then just use the logical expression on that variable and observe the results. It would help illustrate, of course, if you ensure the trial vector has both values in and out of the range.
The point still is regarding the actual question of what it is that is wanted as far as the overall length of the final result??? Since there were two vectors and clearly one was shorter than the other, is the result to be the length of the shorter or the longer? You were at least initially upset that there were zeros for the locations outside the shorter; if that's the case still then as suggested only operate over the minimum of the two lengths. If need the longer but don't want uncertainty in the result by being unable to distinguish zero as a value past the shorter length from zero as a comparison result, then use nan to initialize and those values will be unique from 0/1 and clearly demarcate the difference.

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

카테고리

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