MATLAB Answers

Multiply local values of a matrix with a vector

1 ビュー (過去 30 日間)
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

  0 件のコメント



Sindar 2020 年 7 月 5 日
編集済み: Sindar 2020 年 7 月 5 日
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

  2 件のコメント

Sindar 2020 年 7 月 5 日
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.')
Mateo Landazuri Pachano
Mateo Landazuri Pachano 2020 年 7 月 5 日
Thank you so much, this is very helpful!


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by