multiplying with a with a scaler in a vector element at regular interval
17 次查看(过去 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
0 个评论
采纳的回答
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.
0 个评论
更多回答(1 个)
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!