How to add zeros to 2 x vectors where there are no matches, to get the same length ?
2 views (last 30 days)
Show older comments
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 Comments
Jon
on 29 Jul 2015
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.
Accepted Answer
More Answers (3)
Jon
on 29 Jul 2015
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
on 29 Jul 2015
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 Comments
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!