Makeing two vectors dimentionaly equal, while retaining the info...

2 vues (au cours des 30 derniers jours)
Chris E.
Chris E. le 2 Août 2013
Hello All, Well I have a issue that is seemingly easy, but is not so easy when I'm working on it. I produce 2 column vectors, one is a logic vector and the other is 1:n type vector. I need the amount of columns to be the same. For example:
%I get from the data:
x = [1 2 3 4 5 6 7 8]
y = [0 0 0 1 1 1]
%Because y is smaller I need to change y to:
x = [1 2 3 4 5 6 7 8] %x is the same
y_new = [0 0 0 1 1 1 0 0]
%Or sometimes I get the opposite effect where:
x = [1 2 3 4 5 6 7 8]
y = [0 0 0 1 1 1 0 0 1 1 0 0 1]
%Because x is smaller I need to change x to:
x_new = [1 2 3 4 5 6 7 8 9 10 11 12 13]
y = [0 0 0 1 1 1 0 0 1 1 0 0 1] %y is the same
%The reverse of x and y is true too:
y = [1 2 3 4 5 6 7 8]
x = [0 0 0 1 1 1]
%Because x is smaller I need to change x to:
y = [1 2 3 4 5 6 7 8] %y is the same
x_new = [0 0 0 1 1 1 0 0]
%Or sometimes I get the opposite effect where:
y = [1 2 3 4 5 6 7 8]
x = [0 0 0 1 1 1 0 0 1 1 0 0 1]
%Because y is smaller I need to change y to:
y_new = [1 2 3 4 5 6 7 8 9 10 11 12 13]
x = [0 0 0 1 1 1 0 0 1 1 0 0 1] %x is the same
I just seem to be having a hard time to make the code for this, what I have currently kind of works but does not in all cases. if the smaller vector is 1:n I just need to add the end part so n:m where m is the size of the larger matrix. And if the smaller one is the logical 1's and 0's, I just fill in the end with 0's till it matches the length of the larger one.
Please if you know a good way of doing this, I would appreciate any comments or help anyone offers. This is the code I have and am still working on to see if I can get it, but please note it still does not work properly, however it may help inspire someone to help clean it up or point out where I'm wrong.
switch ifXorY
case 'x'
p = [size(xsub,2); size(ysub,2)];
[numMax v] = max(p);
if v == 2
xsubFix = zeros(1,numMax);
xsubFix(1,1:min(p)) = xsub;
else
ysubFix = 1:length(numMax);
end
% xsubFix = [xsub (length(xsub)+1):(length(ysub))] == 1;
case 'y'
p = [size(xsub,2); size(ysub,2)];
[numMax v] = max(p);
if v == 1
ysubFix = zeros(1,numMax);
ysubFix(1,1:min(p)) = xsub;
else
xsubFix = 1:numMax;
end
% xsubFix = 1:length(ysub);
end

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 2 Août 2013
x = [1 2 3 4 5 6 7 8]
y = logical([0 0 0 1 1 1 0 0 1 1 0 0 1])
nx=numel(x);
ny=numel(y);
ix=islogical(x);
iy=islogical(y);
if nx>ny
if iy==1
y(end+1:nx)=false;
else
y(end+1:nx)=ny+1:nx;
end
elseif nx<ny
if ix==1
x(end+1:ny)=false;
else
x(end+1:ny)=nx+1:ny;
end
end
  2 commentaires
Chris E.
Chris E. le 2 Août 2013
Thank you for your super quick response! It appears that this code works well thank you!
Azzi Abdelmalek
Azzi Abdelmalek le 2 Août 2013
Simplifications
n=max(numel(x),numel(y));
if islogical(y)
y(end+1:n)=false;
x=1:n;
else
y=1:n;
x(end+1:n)=false;
end

Connectez-vous pour commenter.

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 2 Août 2013
Modifié(e) : Azzi Abdelmalek le 2 Août 2013
Another way
Edit
n=max(numel(x),numel(y));
idx=[islogical(x) islogical(y)];
v=[zeros(1,n);1:n];
x(end+1:n)=v(idx,numel(x)+1:n);
y(end+1:n)=v(not(idx),numel(y)+1:n);

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by