Clear Filters
Clear Filters

Save For-loop data to a vector

3 views (last 30 days)
Kole
Kole on 8 Oct 2014
Edited: Stephen23 on 10 Oct 2014
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 Comments
Kole
Kole on 8 Oct 2014
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 on 10 Oct 2014
Edited: Stephen23 on 10 Oct 2014
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

Sign in to comment.

Accepted Answer

Mohammad Abouali
Mohammad Abouali on 8 Oct 2014
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 Comments
Kole
Kole on 8 Oct 2014
Now how can i output the values that are 4 consecutive numbers??
Mohammad Abouali
Mohammad Abouali on 9 Oct 2014
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.

Sign in to comment.

More Answers (1)

Stephen23
Stephen23 on 8 Oct 2014
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)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!