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 个评论
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
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 个评论
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
2014-10-8
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)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!