Multiply local values of a matrix with a vector
2 次查看(过去 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
2
3
4
% 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.')
更多回答(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!