필터 지우기
필터 지우기

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 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