Using two vectors to get the third one
古いコメントを表示
Please I have these three columns where the first line is hour but not all the hours in a day, the second line is data of the corresponding first hours. I have the third line as hours complete.
I want to use the third line, any hour thats is not represented in the first line should be represented with nan in the second line, e.g
0 34 0
1 23 1
2 34 2
4 12 3
5 13 4
7 4.6 5
8 0.4 6
9 -3.8 7
10 -8 8
12 -16.4 9
13 -20.6 10
14 -24.8 11
16 -33.2 12
17 -37.4 13
18 -41.6 14
20 -50 15
21 -54.2 16
23 -62.6 17
18
19
20
21
22
23
2 件のコメント
the cyclist
2019 年 10 月 7 日
How are the data currently stored? When you say "these three columns", do you mean you have three different vectors?
TTA
2019 年 10 月 7 日
採用された回答
その他の回答 (2 件)
the cyclist
2019 年 10 月 7 日
編集済み: the cyclist
2019 年 10 月 7 日
This is virtually equivalent to Fangjun's solution. But it uses some intuitive variable names (and comments) to help you understand what it going on.
The first three lines are where you would have your actual vectors.
%%% This part is just setting up some input data that are like what you describe
incompleteHours = [0; 1; 2; 4; 5; 7]; % Your "first line" hours
data = rand(size(incompleteHours)); % Your "second line" data
completeHours = (0:23)'; % Your "third line" with the complete list of hours
%%% This part is the actual solution, using those inputs
% Preallocate the output with NaN. (We'll fill in the data later)
dataForCompleteHours = nan(size(completeHours));
% Identify the hours we have, and their index to the data
[~,idx] = ismember(incompleteHours,completeHours);
% Fill in the data
dataForCompleteHours(idx) = data;
カテゴリ
ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!