Split vector in groups of data and plot best fit

2 次查看(过去 30 天)
Good evening,
I have a group of data in the following format:
Dat = [ NaN ; NaN ; 23 ; 34 ; 5 ; 12 ; 4 ; 123 ; NaN ; NaN ; Nan ; 23 ; 4 ; 1 ; 4 ; 21 ; Nan ; 234 ; 12 ; 3 ; 12 ; NaN ; 342 ; 32 ; NaN ; NaN ; NaN ; NaN ; 34 ] ;
I would like to take the groups of values between the NaN sets and plot best fit equation. So I would take:
a = [23 34 5 12 4 123 ] b= [23 4 1 4 21] c= etc....
and plot a best fit curve for each of these.
I have managed to come up with a way using indices and a for loop, but it is way to complicated. Also this results to some bugs coming up as we saw with my teacher. So is there any way you would suggest?
KR,
Kosta
  1 个评论
Konstantinos Tsitsilonis
To Clarify, below is the code:
%Condition Matrix = [n x 2] matrix of the format as described above, look 'Dat'.
%Find the number of voyages
for i = 1:2 %number of loading conditions i.e. ballasted and laden
finite_indx = double(~isnan(Condition_Matrix(:,i))) ;
shifted_indx = [0 ; finite_indx(1:end-1) ] ;
multiplied_indx = finite_indx .* shifted_indx ;
Logical_Matrix = [ Condition_Matrix(:,i) , finite_indx , shifted_indx , multiplied_indx ] ;
Voyages_indx = sum( double(Logical_Matrix(:,2:end) == [1 0 0]),2) == 3 ;
Voyages = sum(Voyages_indx) ;
%Indexes
%Index of first data recording value of voyage
idx_low = find(sum( double(Logical_Matrix(:,2:end) == [1 0 0]),2) == 3) ;
%Index of last data recording value of voyage
idx_int = sum( double(Logical_Matrix(:,2:end) == [0 1 0]),2) == 3 ;
idx_high = find([idx_int(2:end) ; true]);
for j=1:Voyages
%Power vs. RPM Matrix of each voyage
P_mat = [P_b(idx_low(j):idx_high(j)) , RPM(idx_low(j):idx_high(j)) , Speed(idx_low(j):idx_high(j))] ;
p(j,:)={P_mat} ;
end
end

请先登录,再进行评论。

采纳的回答

Mudambi Srivatsa
Mudambi Srivatsa 2017-6-26
You can use the following code to split the vector in groups of data:
Dat = [ NaN ; NaN ; 23 ; 34 ; 5 ; 12 ; 4 ; 123 ; NaN ; NaN ; NaN ; 23 ; 4 ; 1 ; 4 ; 21 ; NaN ; 234 ; 12 ; 3 ; 12 ; NaN ; 342 ; 32 ; NaN ; NaN ; NaN ; NaN ; 34 ] ;
count = length(Dat);
% get the indices of NaN elements
nan_indices = find(isnan(Dat));
groups = [];
% pick the elements between NaN elements
for i = 1:length(nan_indices) - 1
% range to pick the elements
first = nan_indices(i) + 1;
last = nan_indices(i+1) - 1;
% pick only if there is atleast one element between NaN values
if( last > first)
groups = [groups {Dat(first:last)}];
end
end
last_nan = nan_indices(end);
% add elements that are after the last NaN
if(count > last_nan)
groups = [groups {Dat(last_nan+1:end)}]
end
The "groups" will have chunks of data between NaNs. In this case, it will have 5 cell arrays. You can access them and use as follows:
a = groups{1}
b = groups{2} and so on..

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by