Save For-loop data to a vector

3 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by