Multiply local values of a matrix with a vector

2 views (last 30 days)
I have a 2880x3200 matrix (A) which is represents 2880 crossections, these crosections grow in size with the exception of certain segments which are separated by 400 elements each. In order to account for that different rate of change, I need to create a 1x2880 vector (B) that will multiply against these segments and alter the value "locally".
Essentially, what I need is to have take the first row of matrix A, just the elements every other 400, skipping the first 400 (400-800, 800-1200, 1200-1600...) and multiply all of them with the first element of vector B. Then again, take the second row of A and multiply only those specific sections with the second number of vector B, and so on until all 2880 crossections have been multiplied against their respective values in vector B. I do not wish to increase the size of matrix A so the multiplication must be done by .* (I believe).
Thank you and happy summer

Accepted Answer

Sindar on 5 Jul 2020
Edited: Sindar on 5 Jul 2020
I'm not quite clear on which columns you want to change, but take a look at this and see if it makes sense:
% sample matrix
>> A = magic(3)
A = 3×3
8 1 6
3 5 7
4 9 2
% sample B
>> B = [2 3 4];
% transpose B to match A's dimensions
>> B.'
ans = 3×1
% update only certain columns of A (here, the 2nd and 3rd)
>> A(:,[2 3]) = A(:,[2 3]).*(B.')
A = 3×3
8 2 12
3 15 21
4 36 8
Sindar on 5 Jul 2020
If you want 400-800,1200-1600,2000-2400,2800-3200, this is one way:
% create a matrix where the rows end at 800,1600,2400,3200, and the preceding elements count up from 400,1200,etc.
idx = (800)*(1:4)'+(-400:0);
% flatten the matrix
idx = idx(:);
% use these as column indices
A(:,idx) = A(:,idx).*(B.')
MLP on 5 Jul 2020
Thank you so much, this is very helpful!

Sign in to comment.

More Answers (0)


Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!