Inserting zeroes where values of another matrix are missing

4 visualizzazioni (ultimi 30 giorni)

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!

Risposta accettata

Stephen23
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
  2 Commenti
Ricardo Higinio Picon Alvaro
Hi, how would this code be if B was to be a column vector instead of a row vector? Cheers
Stephen23
Stephen23 il 7 Mar 2018
Modificato: Stephen23 il 7 Mar 2018
My code will return B with the same size as T, so if T is a column then so is B. Otherwise you can easily convert B to a column:
B = B(:)

Accedi per commentare.

Più risposte (1)

Rik
Rik il 27 Feb 2018
Modificato: Rik il 27 Feb 2018
Create an empty vector with zeros to contain the new B, and look into the ismember function to index it.
  2 Commenti
Rik
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?
Ricardo Higinio Picon Alvaro
The matrix for the time T1 goes round several time as it refers to the time of the day for 365 days. Each one of these times corresponds to a value in B. For example;
T=[1,2,3,1,2,3] and T1=[1,2,1,2,3] where B1=[2,4,3,1,2].
What I need is to create a new matrix for B which replaces the missing values with zeroes (the missing value in this case is the first 3). So the final matrix should be B=[2,4,0,3,1,2]. Hope that helps :)

Accedi per commentare.

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!

Translated by