how do I relate two arrays with a reference point ?

1 回表示 (過去 30 日間)
Seba.V
Seba.V 2019 年 8 月 17 日
回答済み: Bruno Luong 2019 年 8 月 17 日
Given array y=[1 2 3 5 7] and x= [-2 -1 0 1 2 ] I need to generate a code that by inserting n number of zeroz between the y vector (expanding the vector)
ie: y=[1 0 0 2 0 0 3 0 0 5 0 0 7] would also expand the size of the x array by maintaining the zero as the center value x= [-6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 ] so that when the plotting the x&y values would result as a shift of the y values over the x axis with 0 as a reference.
This should work for both even and odd number of lengths of the x and y arrays.
I hope i could the expalnation was clear enough!
Cheers!

採用された回答

Bruno Luong
Bruno Luong 2019 年 8 月 17 日
y=[1 2 3 5 7]; x= [-2 -1 0 1 2 ]
nz = 2; % number of zeros to be inserted
y(end+nz,:) = 0;
y = y(1:end-nz)
x = (0:length(y)-1)-(nz+1)*(find(x==0)-1)

その他の回答 (1 件)

Banister
Banister 2019 年 8 月 17 日
Not the most efficient, but,
if mod(length(y),2)
x = -ceil(length(y)/2):ceil(length(y)/2); % odd vector length
else
x = -length(y)/2:length(y)/2-1; % -1 on the other side to change zero position
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by