how to creat this vector Z?

I have two vector X and Y:
X=[2 5 1 9 3 4 nan nan 4 3 6 9 2 nan nan nan 8 2 13 6 1 nan nan];
Y is the max of each segment
Y=[9 9 13];
I need to creat vector Z
Z=[2 5 1 NAN NAN NAN NAN NAN 4 3 6 NAN NAN NAN NAN NAN 8 2 NAN NAN NAN NAN]

 采纳的回答

Try this:
X=[nan nan 4 3 6 9 2 nan nan nan 8 2 13 6 1 nan];
Y=[9 13];
risingEdgeLocs = find(diff(~isnan(X)) > 0);
indSplit = find(ismember(X,Y));
for i = 1:numel(indSplit)-1
X(indSplit(i):risingEdgeLocs(i+1)) = nan;
end
X(indSplit(end):end) = nan;
Z = X;

4 个评论

Luna thank you for the program but when I tried it, it didn't work
X=[2 5 1 9 3 4 nan nan 4 3 6 9 2 nan nan nan 8 2 13 6 1 nan nan];
Y=[9 9 13];
do you have any solution in order to find this vector Z please ?
Z=[2 5 1 NAN NAN NAN NAN NAN 4 3 6 NAN NAN NAN NAN NAN 8 2 NAN NAN NAN NAN]
Just added a check:
X=[2 5 1 9 3 4 nan nan 4 3 6 9 2 nan nan nan 8 2 13 6 1 nan nan];
Y=[9 9 13];
risingEdgeLocs = find(diff(~isnan(X)) > 0);
indSplit = find(ismember(X,Y));
if numel(risingEdgeLocs)>=numel(indSplit)
for i = 1:numel(indSplit)-1
X(indSplit(i):risingEdgeLocs(i+1)) = nan;
end
else
for i = 1:numel(risingEdgeLocs)
X(indSplit(i):risingEdgeLocs(i)) = nan;
end
end
X(indSplit(end):end) = nan;
Z = X;
Please accept if it covers your problem :)
Luna thank you very much for your answere
Your welcome :)

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by