フィルターのクリア

Save For-loop data to a vector

3 ビュー (過去 30 日間)
Kole
Kole 2014 年 10 月 8 日
編集済み: Stephen23 2014 年 10 月 10 日
I want the Values of i that are returned to be saved into a single vector maybe or somehow display when the data meets my conditions neatly. Here is my script:
clear,clc
load stormdata.txt
S=stormdata;
for i=1:24
if S(i,1)>=30 && S(i,2)<=.5
i
end
end
  3 件のコメント
Kole
Kole 2014 年 10 月 8 日
Now how can i output the values that are 4 consecutive numbers?? the output is a= 9 10 11 12 17 18 19 20 23 24 These are the hours in a day were the storm is considered a blizzard but must be for 4 consecutive hours, so i need to list for example 9-12 and 17-20 is a blizzard
Stephen23
Stephen23 2014 年 10 月 10 日
編集済み: Stephen23 2014 年 10 月 10 日
Finding consecutive sequences of digits like this is easy, and does not require any for loops. This code uses strfind and returns the starting indices of any instances of four consecutive numbers in your vector a:
>> a = [9,10,11,12,17,18,19,20,23,24];
>> strfind(diff(a),[1,1,1])
ans =
1 5
It is also possible to extend this concept with bsxfun (or repmat) to return a matrix of only those consecutive values:
>> n = 4;
>> a = [9,10,11,12,17,18,19,20,23,24];
>> b = strfind(diff(a),ones(1,n-1));
>> c = bsxfun(@plus,b.',0:n-1);
>> a(c)
ans =
9 10 11 12
17 18 19 20

サインインしてコメントする。

採用された回答

Mohammad Abouali
Mohammad Abouali 2014 年 10 月 8 日
do this
load stormdata.txt
S=stormdata;
Idx=find( and( S(:,1)>=30 , S(:,2)<=0.5 ) )
Idx would be the list of "i" that you are looking for
I assummed S has 24 rows, if it has more than that then change it to
Idx=find( and( S(1:24,1)>=30 , S(1:24,2)<=0.5 ) )
  2 件のコメント
Kole
Kole 2014 年 10 月 8 日
Now how can i output the values that are 4 consecutive numbers??
Mohammad Abouali
Mohammad Abouali 2014 年 10 月 9 日
That's a bit tricky and the solution is not that intuitive.
One solution is for-loops (which we is kind of last resort solution).
The other way is this
dIdx=diff(Idx);
dIdx_logical= dIdx==1;
blobs=regionprops(dIdx_logical,'Area','PixelList');
Blobs is the blobs of indices that were 1 index apart. I guess in your case each index is one day or one unit of time so you are looking for detecting events that took at least 4 units of time (4 consecutive numbers).
Anyway, check those blobs that have area 3, by looking at blobs.Area. This means the four indices where only one away from each other. Let's say blob number n had 3, i.e. blobs(3).Area==3. then to get those indices look at blobs(n).PixelList. Not that you would be one index short so your real list of indices is [ blobs(n).PixelList(:,2); blobs(n).PixelList(end,2)+1 ]
So, check it on a small array to see how this is working and then apply it to your longer array.

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
Stephen23 2014 年 10 月 8 日
You should read about vectorization .
Some fake data:
>> S(1:2:24,1) = 100;
>> S(1:3:24,2) = 1;
You can use logical operations on a whole array:
>> S(:,1)>=30 & S(:,2)<=0.5
You can use find if you need the indices as numbers:
>> find(S(:,1)>=30 & S(:,2)<=0.5)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by