help understanding simple Vectorization

Hi,
I am new to vectorization and would appreciate if someone could help me vectorize the following;
for i = 1: length(A)
idx = A(i,1);
C(i) = A(i,1)*B(idx);
end

1 comentario

Fangjun Jiang
Fangjun Jiang el 6 de Ag. de 2011
Please provide an example data. Sometimes the size (of the data) does matter.

Iniciar sesión para comentar.

 Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 6 de Ag. de 2011
C=A(:,1).*B(A(:,1))

3 comentarios

salamay
salamay el 6 de Ag. de 2011
for some reason this didnt work for me
Paulo Silva
Paulo Silva el 6 de Ag. de 2011
I tried and it gave the same result as your code, like Fangjun said you should provide one example for us to test.
salamay
salamay el 6 de Ag. de 2011
im sorry it works
my syntax was wrong
example
A = [1 2; 1 3; 1 6; 2 2; 2 6; 3 9]
B = [2 5 9]

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Simulink en Centro de ayuda y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by