Borrar filtros
Borrar filtros

Adjusting equation based on odd/even row

2 visualizaciones (últimos 30 días)
Sha S
Sha S el 17 de Jul. de 2015
Respondida: Eric Pahlke el 17 de Jul. de 2015
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!

Respuestas (1)

Eric Pahlke
Eric Pahlke el 17 de Jul. de 2015
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

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by