Adjusting equation based on odd/even row

조회 수: 1 (최근 30일)
Sha S
Sha S 2015년 7월 17일
답변: Eric Pahlke 2015년 7월 17일
Hi,
I have 2 matrices. Matrix A is 104x15 and matrix B is 103x1.
I want to add element one in matrix B to the value in row 1 column 4 in matrix A. Then I want to add element 2 in Matrix B to the value in row 2 column 7 in matrix A. Then I want to add element 3 in matrix B to the value in row 3 column 4 in matrix A. Then I want to add element 4 in matrix B to the value in row 4 column 7 in matrix A....and so on. I want to do this for the first 103 rows (I realize I can't do it for 104 rows because matrix B only goes up until row 103).
I pretty much want to add the odds rows in matrix B to column 4 of all the odd rows in matrix A. And then I want to add the even rows in matrix B to column 7 of all the even rows in matrix A. In the end I want one matrix that is 103x1.
Can someone please help me do this. Thank you!

답변 (1개)

Eric Pahlke
Eric Pahlke 2015년 7월 17일
A = zeros(104,15);
A(:,4) = 1; % Just to distinguish column 4 from column 7
A(:,7) = 101; % Just to distinguish column 4 from column 7
B = (1:103)'; % Define a 103x1 B matrix
C = zeros(size(B)); % Preallocate the new matrix so that we can use the "end" operator
% Odd values: 1:2:end means start at element 1, increment by 2, continue to the end of the array
% Even values: 2:2:end means start at element 2, increment by 2, continue to the end of the array
C(1:2:end) = A(1:2:length(B),4) + B(1:2:end); % Odd
C(2:2:end) = A(2:2:length(B),7) + B(2:2:end); % Even

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by