multiplying with a with a scaler in a vector element at regular interval
17 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Chaudhary P Patel
am 28 Jun. 2023
Bearbeitet: Jacob Mathew
am 28 Jun. 2023
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 Kommentare
Akzeptierte Antwort
Jacob Mathew
am 28 Jun. 2023
Bearbeitet: Jacob Mathew
am 28 Jun. 2023
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 Kommentare
Weitere Antworten (1)
Aman
am 28 Jun. 2023
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 Kommentare
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!