Inserting zeroes where values of another matrix are missing

6 ビュー (過去 30 日間)
Ricardo Higinio Picon Alvaro
Ricardo Higinio Picon Alvaro 2018 年 2 月 27 日
編集済み: Stephen23 2018 年 3 月 7 日

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!

採用された回答

Stephen23
Stephen23 2018 年 2 月 28 日
編集済み: Stephen23 2018 年 3 月 7 日
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 件のコメント
Ricardo Higinio Picon Alvaro
Ricardo Higinio Picon Alvaro 2018 年 3 月 7 日
Hi, how would this code be if B was to be a column vector instead of a row vector? Cheers
Stephen23
Stephen23 2018 年 3 月 7 日
編集済み: Stephen23 2018 年 3 月 7 日
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(:)

サインインしてコメントする。

その他の回答 (1 件)

Rik
Rik 2018 年 2 月 27 日
編集済み: Rik 2018 年 2 月 27 日
Create an empty vector with zeros to contain the new B, and look into the ismember function to index it.
  2 件のコメント
Rik
Rik 2018 年 2 月 27 日
"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
Ricardo Higinio Picon Alvaro 2018 年 2 月 28 日
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 :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by