You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
A number contain in a series.
1 view (last 30 days)
Show older comments
I have a series 's'
s=(1:1,2:end)
I find its subsequences like
a=s(1:30)
b=s(30:60)
c=s(60:90)
d=s(90:120)
e=s(120:150)
f=s(150:180)
j=s(180:210)
h=s(210:250)
and I have a set of points 'k'.
I need to check any of the subsequence contain any points of the K.If it contain I need to add those subsequence into a variable 'A'.How can I do this.Please help me.
Answers (1)
thoughtGarden
on 19 Sep 2019
Edited: thoughtGarden
on 20 Sep 2019
You haven't provided enough information to be certain of what you want, but makeing some assumptions, this should work.
clear;clc;
% "series" s, which is an array
s = 1:1:250;
% Build "subsequences"
for ii = 1:7
subSequence(ii,:) = s((ii-1)*30 + 1:ii*30);
end
% Build "set of points 'k'"
k = randi(1000,1,10);
% A will contain all subsequences that contain any of the values of k
A = [];
% In each loop, determine if there is any overlap betweent he subsequence
% and the variable k. If there is, add the subsequency to the variable A.
% Otherwise, move on to the next subsequence.
for ii = 1:length(subSequence(:,1))
if(intersect(subSequence(ii,:),k))
A(end+1,:) = subSequence(ii,:);
else
%do nothing...
end
end
% Disp A, which in the case of this script might be empty as k is random
% values...
disp(A)
This builds an array containing all the subsequences that contain any values found in k. If that is what you are looking for, this works.
23 Comments
Guillaume
on 19 Sep 2019
Note that
if intersect(subSequence{ii},k) %no need for extra brackets
would be better written as
if ~isempty(intersect(subSequence{ii},k))
as that's what your if expression will evaluate to and I think what you meant. In particular your if will be false if the intersection is the vector [0].
Even better would be to use ismember as you don't actually care about computing the intersection:
if any(ismember(subSequence{ii}, k))
thoughtGarden
on 19 Sep 2019
You are correct on all accounts. Answer updated.
Silpa K
on 20 Sep 2019
d = xlsread('FaceFour_TRAIN.xlsx')
s = d(1:1,2:end );
fa = movstd(s,20 );
secarray = movstd(fa,20 ) ;
sec = secarray(secarray>.04 );
k=maxk(sec,14);
a=s(1:30)
b=s(30:60)
c=s(60:90)
d=s(90:120)
e=s(120:150)
f=s(150:180)
j=s(180:210)
Guillaume
on 20 Sep 2019
the nearest value
the value of what nearest to the value of what?
thoughtGarden() has shown you an efficient way of storing the subsequences, using indices of a cell array. Using sequentially named variables (a, b, c, ...) as you have done is an extremely bad way of writing code. Use indexing instead.
thoughtGarden
on 20 Sep 2019
After looking at your code, it looks like you are trying to find the subsequences that contain any of the max 14 values of the variable sec. If you use the template above (note I removed the cells as it appears you have constant and equal length subsequences) you can solve the problem. I have not used your sequencial lettering of variables because in general this is poor practice (what if you your subsequence number grows in the future?). However, if k is some array containing values that are also present in the subsequences, the for loop will find them.
d = xlsread('FaceFour_TRAIN.xlsx')
s = d(1:1,2:end );
fa = movstd(s,20 );
secarray = movstd(fa,20 ) ;
sec = secarray(secarray>.04 );
k=maxk(sec,14);
% Build "subsequences"
for ii = 1:7
subSequence(ii,:) = s((ii-1)*30 + 1:ii*30);
end
% A will contain all subsequences that contain any of the values of k
A = [];
% In each loop, determine if there is any overlap betweent he subsequence
% and the variable k. If there is, add the subsequency to the variable A.
% Otherwise, move on to the next subsequence.
for ii = 1:length(subSequence(:,1))
if(intersect(subSequence(ii,:),k))
A(end+1,:) = subSequence(ii,:);
else
%do nothing...
end
end
% Disp A, which in the case of this script might be empty as k is random
% values...
disp(A)
Pending you are reading the excel file correctly, this should work.
thoughtGarden
on 20 Sep 2019
A has as many rows as subsequences that contained data found in k. If 3 subsequences contain data found in k, then A has 3 rows. A thus holds all the subsequences of interest. I'm not sure what the variable W is, but I think all you want (according to your OP) is A.
thoughtGarden
on 20 Sep 2019
What is the error? How big is s? you previously indicated that s has 250 elements and then 210. If either of these values are valid, you should see no issue.
thoughtGarden
on 20 Sep 2019
Please copy and past the error.
Silpa K
on 20 Sep 2019
A has as many rows as subsequences that contained data found in k. If 3 subsequences contain data found in k, then A has 3 rows. A thus holds all the subsequences of interest. I'm not sure what the variable W is, but I think all you want (according to your OP) is A.
Depending on this answer,
Sir I want to display the sequence names that contain the points.
thoughtGarden
on 20 Sep 2019
See my comment above. I switched from cells to data array as it appeared you didn't need the extra overhead of cells as each subsequence is the same length.
d = xlsread('FaceFour_TRAIN.xlsx')
s = d(1:1,2:end );
fa = movstd(s,20 );
secarray = movstd(fa,20 ) ;
sec = secarray(secarray>.04 );
k=maxk(sec,14);
% Build "subsequences"
for ii = 1:7
subSequence(ii,:) = s((ii-1)*30 + 1:ii*30);
end
% A will contain all subsequences that contain any of the values of k
A = [];
% In each loop, determine if there is any overlap betweent he subsequence
% and the variable k. If there is, add the subsequency to the variable A.
% Otherwise, move on to the next subsequence.
for ii = 1:length(subSequence(:,1))
if(intersect(subSequence(ii,:),k))
A(end+1,:) = subSequence(ii,:);
else
%do nothing...
end
end
% Disp A, which in the case of this script might be empty as k is random
% values...
disp(A)
thoughtGarden
on 20 Sep 2019
The code in my just previous comment throws no errors (I just ran it with your data set). However, k doesn't have any overlapping values with any of your subsequences:
k =
Columns 1 through 10
0.4889 0.4823 0.4815 0.4719 0.4705 0.4577 0.4553 0.4393 0.4341 0.4175
Columns 11 through 14
0.4037 0.4035 0.4015 0.3986
and your subsequences are all whole numbers, thus no overlap. Did you intend your subsequences to have only whole nubmers? Did you indend k to have only whole numbers? This discrempancy must be resolved before more progress can be made.
thoughtGarden
on 20 Sep 2019
Right, but no values in k are present int he subsequence you've indicated. The subsequences you have provided are all whole numbers, but the values in k are not. Thus there is no overlap between any value in k and any subsequence. You either need different k or different subsequence.
See Also
Categories
Find more on Data Type Conversion 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!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)