フィルターのクリア

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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by