Inserting zeroes where values of another matrix are missing
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Ricardo Higinio Picon Alvaro
il 27 Feb 2018
Modificato: Stephen23
il 7 Mar 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 Commenti
Risposta accettata
Stephen23
il 28 Feb 2018
Modificato: Stephen23
il 7 Mar 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
Più risposte (1)
Rik
il 27 Feb 2018
Modificato: Rik
il 27 Feb 2018
2 Commenti
Rik
il 27 Feb 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?
Vedere anche
Categorie
Scopri di più su Creating and Concatenating Matrices in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!