필터 지우기
필터 지우기

Separating vectors into individual subsets for plotting

조회 수: 1 (최근 30일)
Donald John
Donald John 2013년 8월 5일
I have a vector with a series of data values from 0 to 30. I want to split the vector into separate parts so that each data range from 0 to 30 is seperated and subsequently I can plot the seperate data series. How would I go about doing this?
My vector looks like this:
10 20 30 NaN 30 20 10 NaN 10 20 30
Its on the basis of the NaN that I want to split the vector.
Thanks in advance.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 5일
Using a for loop is faster
if ~isnan(a(end))
a(end+1)=nan;
end
idx2=find(isnan(a));
idx1=[1 idx2(1:end-1)+1];
n=numel(idx1);
out2=cell(n,1);
for k=1:n
out2{k}=a(idx1(k):idx2(k)-1);
end

추가 답변 (4개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 5일
a=[10 20 30 NaN 30 20 10 NaN 10 20 30]
a(isnan(a))=[]
out=reshape(a,3,[])
  댓글 수: 2
Jan
Jan 2013년 8월 5일
편집: Jan 2013년 8월 5일
@Donald: Please post comments in the comment section.
[EDITED, relocated comment] Donald John wrote:
Thanks for this - however in reality my vector has an unknown number of separate ranges. How would I go about separating this data if I do not know how many individual ranges from 10 to 30 there are?
Also I would ideally like to output the result as separate vectors.
Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 5일
a=[10 20 30 NaN 30 20 10 NaN 10 20 30]
if ~isnan(a(end))
a(end+1)=nan
end
idx2=find(isnan(a))
idx1=[1 idx2(1:end-1)+1]
out=arrayfun(@(ii1,ii2) a(ii1:ii2),idx1,idx2-1,'un',0)

댓글을 달려면 로그인하십시오.


nah
nah 2013년 8월 5일
a=[10 20 30 NaN 30 20 10 NaN 10 20 30 0 10 20 30 NaN 30 20 10 NaN 10 10 ]
aaNaNidx = find(isnan(a));
for ix = 2:length(aaNaNidx)
ll = aaNaNidx(ix-1)+1: aaNaNidx(ix);
splitVect{ix,1} = aa(ll);
end
splitVects{1,1} = aa(1:aaNaNidx(1));
splitVects{end+1,1} = aa(aaNaNidx(end)+1:length(aa));
Since you are saying that the vector will be unevenly split, it needs to be stored in an different data structure called Cells;
  댓글 수: 3
Azzi Abdelmalek
Azzi Abdelmalek 2013년 8월 5일
What is aa?
Jan
Jan 2013년 8월 5일
It is a typo and "a" is meant. This might be an effect of the 33 C in my office.

댓글을 달려면 로그인하십시오.


Donald John
Donald John 2013년 8월 5일
Okay, I have two further questions.
1. Is it possible to plot the new cell structure and how would I do this?
2. Can I do something similar if my data is in fact in a matrix structure, such as below?
650 10
700 20
750 30
NaN NaN
630 30
640 20
650 10
NaN NaN
740 10
720 20
715 30
NaN NaN
700 30
etc etc
  댓글 수: 2
Jan
Jan 2013년 8월 5일
Please post new questions in a new thread. Otherwise the method to accept an answer is not useful anymore.
You would plot the separated data in a FOR loop and yes, the shown method can be applied to matrices also with tiny modifications. Just try it and post again if problems occur.
Donald John
Donald John 2013년 8월 5일
I thought that this followed on from my previous question. To be honest I am still struglling to get this to work, especially now that I am using matrices. Do you have any further advice?

댓글을 달려면 로그인하십시오.


Andrei Bobrov
Andrei Bobrov 2013년 8월 5일
편집: Andrei Bobrov 2013년 8월 5일
z = [nan nan nan nan 1 3 5 nan nan 3 4 nan 6 0 3 nan nan]';
z = z(:);
if ~isnan(z(1)), z = [nan; z]; end;
z1 = z(find(~isnan(z),1,'first')-1:end);
a = zeros(size(z1));
b = ~isnan(z1(:));
a(strfind(~b(:)',[1 0])) = 1;
a1 = cumsum(a);
out = accumarray(a1(b),z1(b),[],@(x){x});

Community Treasure Hunt

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

Start Hunting!

Translated by