Adjusting equation based on odd/even row
2 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(1 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!