# Replacing valuees in a matrix

2 visualizaciones (últimos 30 días)
Dominika el 19 de Mzo. de 2014
Respondida: Dominika el 20 de Mzo. de 2014
Hi
I need to replace values in a vector bg_correction with values from vector Sh_a_po if a condition:
Sh_a_BG <= 0.1*Sh_a_po
is satisfied (for each cell separately)
I have 3 vectors:
%%---
bg_correction =
0.1120
0.0495
0.0261
0.0158
0.0393
0.0096
Sh_a_BG =
0.0008
0.0004
0.0007
0.1698
0.1756
0.0114
Sh_a_po=
0.0495
0.0261
0.0160
0.0395
0.0096
0.0035
%if Sh_a_BG <= 0.1*Sh_a_po --> replace bg_correction with Sh_a_po
bg_correction(Sh_a_BG_po<=0.1*Sh_a_po) = Sh_a_po1;
%%
The last line doesn't work
Message at Command Window:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Thanks,
Dominika
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Joseph Cheng el 19 de Mzo. de 2014
Editada: Joseph Cheng el 19 de Mzo. de 2014
I would suggest use the find() function. With find you can get the index of values where it is Sh_a_BG_po<=0.1*Sh_a_po.
index = find(Sh_a_BG_po<=0.1*Sh_a_po);
bg_correction(index)=Sh_a_po1(index);
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Joseph Cheng el 19 de Mzo. de 2014
additionally your original last line would work but you need to include the (Sh_a_BG_po<=0.1*Sh_a_po) on both sides of the equation.
bg_correction(Sh_a_BG_po<=0.1*Sh_a_po) = Sh_a_po1(Sh_a_BG_po<=0.1*Sh_a_po);

Iniciar sesión para comentar.

### Más respuestas (1)

Dominika el 20 de Mzo. de 2014
Thank you!
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

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

### Community Treasure Hunt

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

Start Hunting!

Translated by