How to assign vectors to existing vector

69 ビュー (過去 30 日間)
karishma koshy
karishma koshy 2019 年 8 月 7 日
編集済み: madhan ravi 2019 年 9 月 15 日
Hi all
I want to assign a set of vectors in place of zero to existing vectors (ventor_2). But I always want to check the the new set of vectors (newvector_2) does not have the number that exist in previous vector (vector_1)
Vector_1 = [ 1 2 3 4]
Vector_2 = [ 2 0 4 0 6 7 ] Newvector_2 = [ 2 5 4 8 6 7]
How can I do that
Thank you
  4 件のコメント
karishma koshy
karishma koshy 2019 年 8 月 7 日
Yes, it should be in the chronological order
karishma koshy
karishma koshy 2019 年 8 月 7 日
編集済み: karishma koshy 2019 年 8 月 7 日
@Guillaume I have got a set vectors vector_1 before vector_2. So I want to confirm that newly assigned numbers to newvector_2 are not present in vector_1. I know how to check for a single number . But how can I do it for all the numbers in the vector?

サインインしてコメントする。

回答 (3 件)

Samatha Aleti
Samatha Aleti 2019 年 8 月 12 日
Without logically going to each element you can use functions like find(), intersect(), setxor() for vectorization in achieving what you need. Here is how you can do it:
vector_1 = [ 1 2 3 4];
vector_2 = [ 2 0 4 0 6 7];
% x is the vector whose elements are to be added to vector_2
x = [1 2 5 8];
xcommon = intersect(x, vector_1);
vector_3 = setxor(x,xcommon);
idxZero = find(~ vector_2);
% Truncate additional elements
vector_3(length(idxZero)+1:length(vector_3)) = [];
vector_2(idxZero) = vector_3;
For detailed information on above functions, refer the following document links:
  2 件のコメント
madhan ravi
madhan ravi 2019 年 8 月 12 日
Where did x come from?
Samatha Aleti
Samatha Aleti 2019 年 8 月 12 日
I took "x" as an example here. In your case "x" is the vector set whose elements are to be added in place of zeros of vector_2.

サインインしてコメントする。


madhan ravi
madhan ravi 2019 年 8 月 12 日
編集済み: madhan ravi 2019 年 9 月 15 日
newvector_2=vector_2;
ix=nnz(~vector_2);
v12=[vector_1,nonzeros(vector_2).'];
u=unique(v12);
d=setdiff(1:max(v12),v12);
newvector_2(~vector_2)=[d,max(v12)+(1:ix-numel(d))]

Andrei Bobrov
Andrei Bobrov 2019 年 8 月 12 日
Let X - interval for input data in 'vector_2'
X = [0, 15];
x = setdiff(X(1):X(2),vector_1); % input data
newvector_2 = vector_2;
lo = vector_2 == 0;
n = sum(lo);
b = repmat(x,1,ceil(n/numel(x)));
newvector_2(lo) = b(randperm(numel(b),n));

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by