필터 지우기
필터 지우기

Multiply local values of a matrix with a vector

조회 수: 2 (최근 30일)
MLP
MLP 2020년 7월 4일
댓글: MLP 2020년 7월 5일
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

채택된 답변

Sindar
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
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.')
MLP
MLP 2020년 7월 5일
Thank you so much, this is very helpful!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by