Multiplying every secound element in a vector with -1
古いコメントを表示
Hello I am looking for how to multiply every secound element in vector 1xn with -1, but I want to write this as a function. I can do this manually like this for example with an 1x3 vector;
function vny = byt_tecken(v)
vny=[v(1,1),-v(1,2),v(1,3)]
end
But how do I do this for any given 1xn vector? Thanks!
採用された回答
その他の回答 (2 件)
Daniel M
2019 年 10 月 3 日
Here is another way:
function vny = byt_tecken(v)
vny = v.*-(-1).^(1:numel(v));
end
James Tursa
2019 年 10 月 3 日
Or yet another of the zillion ways
vny = v;
vny(2:2:end) = -vny(2:2:end);
カテゴリ
ヘルプ センター および File Exchange で Get Started with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!