필터 지우기
필터 지우기

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

조회 수: 6 (최근 30일)
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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by