Store values in cells based on their potition.
1 view (last 30 days)
Show older comments
PANAGIOTIS GEORGIOS ILIOPOUOS
on 26 Mar 2022
Commented: PANAGIOTIS GEORGIOS ILIOPOUOS
on 26 Mar 2022
I have a matrix A=[1,2,3,6,7,8,11,12,13,14,15,17,18,19,30,31,32 ; 30,38,42,56,64,82,96,67,88,46,37,94,65,82,97,106,89]] A=(2x17)
The values of the firtst row are stored, baced on vector jumps, in cell array like :
C = {[1 2 3]} {[6 7 8]} {[11 12 13 14 15]} {[17 18 19]} {[30 31 32]}
how can i find the potition that each one of these values have correspondence with the first matrix in order to also store the values of the second row in commensurable potitions?
The output i want is :
V = {[30,38,42]} {[56,64,82]} {[96,67,88,46,37]} {[94,65,82]} {[97,106,89]}
P.S what's the difference if the first row of the matrix A is made up of ''date time'' values? (I thinks i can not use the command ''find'').
0 Comments
Accepted Answer
Voss
on 26 Mar 2022
Edited: Voss
on 26 Mar 2022
Calculate V at the same time as C, using the same indices dervied from the first row of A, but applied to the second row:
A = [ ...
1, 2, 3, 6, 7, 8,11,12,13,14,15,17,18,19,30, 31,32; ...
30,38,42,56,64,82,96,67,88,46,37,94,65,82,97,106,89];
C = {};
V = {};
ind = [1 find(diff(A(1,:)) ~= 1)+1 size(A,2)+1];
for n = 1:numel(ind)-1
C{n} = A(1,ind(n):ind(n+1)-1);
V{n} = A(2,ind(n):ind(n+1)-1);
end
C
V
P.S for a datetime array, I guess you will have to define what "consecutive" means. I mean, in this case using positive integers, "consecutive" is naturally defined as differing by 1, but for datetime arrays, should elements differing by 1 day be considered "consecutive"? Those differing by 1 second? 1 month? How about 6 weeks, why not?
3 Comments
AndresVar
on 26 Mar 2022
@PANAGIOTIS GEORGIOS ILIOPOUOS here is alternative using ismember: Array elements that are members of set array - MATLAB ismember (mathworks.com)
A=[1,2,3,6,7,8,11,12,13,14,15,17,18,19,30,31,32 ;
30,38,42,56,64,82,96,67,88,46,37,94,65,82,97,106,89];
C = {[1 2 3],[6 7 8],[11 12 13 14 15],[17 18 19],[30 31 32]}
Asplit = C; % pre allocates
for ii = 1:numel(C)
Asplit{ii}=A(2,ismember(A(1,:),C{ii}));
end
Asplit
can also try using the second output from intersect: Set intersection of two arrays - MATLAB intersect (mathworks.com)
also if C is always going to be a running string maybe you can use bin edges instead and use discretize.
More Answers (0)
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!