How to add zeros to 2 x vectors where there are no matches, to get the same length ?
조회 수: 2 (최근 30일)
이전 댓글 표시
x=[1 2 3 4];
y=[1 2 3 1];
x1=[3 4 5 6 7 8];
y1=[2 6 4 2 5 3];
[x, y, x1, y1] = Tout_Ajuster(x, y, x1, y1)
I want to put zeros where there is no match in x axes to get the same length for (x and x1) so I write a small code (Tout_Ajuster.m) in the output, x1 misses (5 and 6) so y1 miss(4 and 2) What is missing in my function < Tout_Ajuster.m > ???
댓글 수: 2
Jon
2015년 7월 29일
It would help if you gave us the expected outputs for x1 and y1. Otherwise it's not clear what you're trying to do.
채택된 답변
추가 답변 (3개)
Jon
2015년 7월 29일
Your question seems ambiguous to me, but based off my interpretation, see if this code gives you the result you're looking for:
% This assumes that none of the matching x values are 0
N = max(numel(x),numel(x1));
xout = zeros(1,N);
yout = zeros(1,N);
x1out = zeros(1,N);
y1out = zeros(1,N);
% Reinsert the known values of x and y
xout(1:numel(x)) = x;
yout(1:numel(y)) = y;
% Values in x1 (and the correponding y1 positions) that do not appear in x are set to zero
for i = 1:numel(x)
matching = find(x(i) == x1);
if isempty(matching) == 1
continue
else
x1out(matching) = x1(matching);
y1out(matching) = y1(matching);
end
end
Gives the following results:
xout =
1 2 3 4 0 0
yout =
1 2 3 1 0 0
x1out =
3 4 0 0 0 0
y1out =
2 6 0 0 0 0
Azzi Abdelmalek
2015년 7월 29일
n=max(numel(x),numel(x1))
x=[x zeros(1,n)]
x1=[x1 zeros(1,n)]
y=[y zeros(1,n)]
y1=[y1 zeros(1,n)]
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!