フィルターのクリア

multiplying with a with a scaler in a vector element at regular interval

8 ビュー (過去 30 日間)
Chaudhary P Patel
Chaudhary P Patel 2023 年 6 月 28 日
編集済み: Jacob Mathew 2023 年 6 月 28 日
I have a 15X1 size vector, i want to multiply with a scaler to the element of vector at a regular place i.e., 3, 6, 9, 12, 15

採用された回答

Jacob Mathew
Jacob Mathew 2023 年 6 月 28 日
編集済み: Jacob Mathew 2023 年 6 月 28 日
Hi Chaudhary,
As mentioned by Aman, you can index into the vector and multiply by a scalar:
indices = [2 4 6 8 10];
vector(indices) = vector(indices) * scalar;
This means that it doesn't have to be regular or follow any pattern i.e the index vector can contain any values and those indices will only be affected by the operation.
However, you have mentioned that you want to multiply at regular intervals. In case this interval is dynamic then you can use the semicolon ( : ) operator to generate the indices at the interval that you want.
Conside you have a variable n that you take as input. and then you want to multply the vector at every 3rd index till n i.e 3, 6, 9, ... n. In such cases consider the following:
n = 15; % This is input. Set to what you want
vector = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]';
scalar = 10;
vector(3:3:n) = vector(3:3:n) * scalar;
% vector(start : offset : end ) is the usage
This solution will scale to vectors of any size and any interval constraints.

その他の回答 (1 件)

Aman
Aman 2023 年 6 月 28 日
Hi,
To multiply specific elements of a 15x1 vector by a scalar at regular intervals (3, 6, 9, 12, 15), you can use indexing to access those elements and perform the multiplication.
Here's an example code snippet that demonstrates how to do this:
% Create a 15x1 vector
vector = (1:15)';
% Define the scalar value
scalar = 2;
% Specify the indices of the elements to be multiplied
indices = [3, 6, 9, 12, 15];
% Multiply the specified elements by the scalar
vector(indices) = vector(indices) * scalar;
Hope this helps!

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by