Inserting zeroes where values of another matrix are missing
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ricardo Higinio Picon Alvaro
el 27 de Feb. de 2018
Hi, I wanted to know how to insert zeroes where values for a matrix are missing. Values are known to be missing by comparing a time matrix to another. It is very important that the values are inserted and do not replace the previous ones. As seen in the example the final matrix should be as large as the time matrix. Below there is a picture with a small explanation. Thank you!
0 comentarios
Respuesta aceptada
Stephen23
el 28 de Feb. de 2018
Editada: Stephen23
el 7 de Mzo. de 2018
T = [1,2,3,1,2,3,1,2,3];
T1 = [1,2,1,2,3,2,3];
B1 = [2,4,3,1,2,6,7];
idx = bsxfun(@eq,T1(:).',T(:));
idy = false(size(T));
off = 0;
for k = 1:numel(T)
if idx(k,k-off)
idy(k) = true;
else
off = off+1;
end
end
B = +idy;
B(idy) = B1
giving:
B =
2 4 0 3 1 2 0 6 7
Más respuestas (1)
Rik
el 27 de Feb. de 2018
Editada: Rik
el 27 de Feb. de 2018
2 comentarios
Rik
el 27 de Feb. de 2018
"Thanks for the answer, but it wasn't quite what I was looking for, I didn't explain it correctly. The time matrix function goes round more than once, so the example probably holds better if matrices T1 and T go from 1 to 5 twice. T=[1;2;3;4;5;1;2;3;4;5] and T1 still has missing values. Matrix B is not composed only of zeroes either that was just for simplification but it has caused confusion."
Please only use the answer field for actual answers. Their order can change, so they aren't a good choice to keep the topics organized.
I meant you needed to pre-allocate the vector B_new with only zeros and then fill the positions that T and T1 share with values from B. You can find those postions by using ismember, as one of the outputs is the indexing vector you need.
This suggestion ignores the possibility of either vector containing repeats. What should happen in such a case? Should only one be used? Or mean value? Or something different?
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices 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!