Using two vectors to get the third one

3 次查看(过去 30 天)
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
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?

请先登录,再进行评论。

采纳的回答

Joe Vinciguerra
Joe Vinciguerra 2019-10-7
x = [0,1,2,4,5,7,8,9,10,12,13,14,16,17,18,20,21,23]; % here's your first column
y = x*rand()+rand(); % here's a vague representation of your second column
z = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23]; % here's your third column
index = (ismember(z,x)); % find which values in z are in x
yPrime = zeros(length(index),1); % preallocate a new variable
skip = 0; % We need a counter to count every time we skip a value to make the array sizes work out
for i = 1:length(index)
if index(i) % if the number exists...
yPrime(i) = y(i-skip); % stuff it in a new variable
else % if it doesn't exist
yPrime(i) = NaN; % set it to NaN
skip = skip + 1; % and count it
end
end
  8 个评论
TTA
TTA 2019-10-9
Thank you Joe ..................Please help me take a look at it again. It didnt work.
Grazie mille
Walter Roberson
Walter Roberson 2019-10-9
Joe Vinciguerra comments to Toyese Ayorinde
This code works with file provided, and does what was requested. This question should be closed. Toyese, if you have addition issues you should ask a new question and provide details about what you are trying to accomplish and specifically what the issue you are having is. Thank you.

请先登录,再进行评论。

更多回答(2 个)

Fangjun Jiang
Fangjun Jiang 2019-10-7
a=[0 1 4 7]
b=rand(size(a))
c=0:23
d=nan(size(c))
d(a+1)=b
  2 个评论
TTA
TTA 2019-10-7
Thanks but please what if I don't these hours that are missing?

请先登录,再进行评论。


the cyclist
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;
  2 个评论
TTA
TTA 2019-10-8
Hi, please this solved the question I asked but unfortunately I dont know how to apply it to my problem.
What i want to do is this.....looking at the attached file, column 5 is the hour and 6 is the minutes, and 12 is the data i want to use. But it is not every hour of the day (0-23) that has data represented. Therefore I want to make every hour where the data is not represented to be NAN.
I some other files I don't these hours in particular.
here is the code I'm using
%% Initialize variables.
filename = 'C:\Users\INPE\Documents\Incomplete24\Data_Tropopause_08-Sep-2006.txt';
formatSpec = '%4f%5C%4f%5f%4f%4f%7f%8f%15f%15f%15f%f%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string', 'ReturnOnError', false);
%% Close the text file.
fclose(fileID);
%% Create output variable
Data = table(dataArray{1:end-1}, 'VariableNames', {'Year','Month','Date','DayofYear','Hour','Minute','Latitude','Longitude','TropTempRaw','TropHeightRaw','TropTempTb','TropHeightTb'});
%% Clear temporary variables
clearvars filename formatSpec fileID dataArray ans;
x = table2array(Data(:,12));
ss1 = num2str(table2array(Data(:,5)));
ss2 = num2str(table2array(Data(:,6)));
SS1 = table2array(cell2table(strcat(ss1, {'.'}, ss2)));
SS2 = str2double(regexprep(SS1,' ',''));
%x = [0,1,2,4,5,7,8,9,10,12,13,14,16,17,18,20,21,23]; % here's your first column
%y = Data(:,12); % here's a vague representation of your second column
%z = 0:1:23; % here's your third column
z= (0:1439)./60;
index = (ismember(z,x)); % find which values in z are in x
yPrime = zeros(length(index),1); % preallocate a new variable
skip = 0; % We need a counter to count every time we skip a value to make the array sizes work out
for i = 1:length(index)
if index(i) % if the number exists...
yPrime(i) = SS2(i-skip); % stuff it in a new variable
else % if it doesn't exist
yPrime(i) = NaN; % set it to NaN
skip = skip + 1; % and count it
end
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by