6 views (last 30 days)

Hello everyone,

I have two matrices, kindly find the attached file

soho ---> size = 23×12 , each row represents an event.

omni ---> size = 8760 × 17 , each row represent an hour (timeseries data).

The first three columns are, in order, "year", "month", "day".

I need to do the following operation:

if soho_day = omni_day

take that omni_day and the following 120 rows (hours) and put them as a separate matrix with a prefix (i.e., omniSUB_1).

and so on

I have these code blocks

% to find the matched date and store the that day along with the following 5 days (120 hours) in another matrix.

for n = 1:length(soho)

for m = 1:length(omni)

if datetime(soho(n,1),soho(n,2),soho(n,3),...

'Format','dd/MM/yyyy') == ...

datetime(omni(m,1),omni(m,2),omni(m,3),...

Format','dd/MM/yyyy')

for k = m:120

omniSUB{n,:} = omni(k,:);

end

end

end

end

and

% to create sub-matrices.

Prefix = 'omniSUB_';

for i = 1:Month_length

var_name = strcat(Prefix, num2str(i));

data_child = genvarname(var_name);

eval([data_child ' = omniSUB{i}']);

end

but I don't know how to put them together to do that operation. Please correct me.

I appreciate your help.

Thanks in advance

Ridwan Alam
on 21 Dec 2019

Edited: Ridwan Alam
on 21 Dec 2019

% to find the matched date and store the that day along with the following 5 days (120 hours) in another matrix.

for n = 1:size(soho,1)

omniRowInd = find(omni(:,1)==soho(n,1) & omni(:,2)==soho(n,2) & omni(:,3)==soho(n,3),1,'first');

if ~isempty(omniRowInd)

tempTable = omni(omniRowInd:min(omniRowInd+119,size(omni,1)),:);

eval(['omniSUB_' num2str(n) '=' 'tempTable;']);

end

end

Hope this helps!

Stephen Cobeldick
on 23 Dec 2019

"Could you determine the problem here plz? "

Are you checking all of the cells of the cell array, not just the last one?:

find(~cellfun(@isempty,C))

It is quite possible that the if condition is never true. You can check this by printing it in the loop, or collect it into a vector and checking it after the loop:

N = size(soho,1);

C = cell(1,N);

L = false(1,N); % logical vector

for k = 1:N

omniRowInd = find(omni(:,1)==soho(k,1) & omni(:,2)==soho(k,2) & omni(:,3)==soho(k,3),1,'first');

if ~isempty(omniRowInd)

L(k) = true; % if the condition is true

C{k} = omni(omniRowInd:min(omniRowInd+119,end),:);

end

end

% Check on which iteration/s the conditions was true:

any(L)

find(L)

Note how easy it is using indexing to access the new variable L. Trying to use eval to do something similar with lots of numbered variables would be much more complex.

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

Start Hunting!
## 4 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/497661-how-to-extract-sub-matrices-from-a-big-matrix#comment_780085

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/497661-how-to-extract-sub-matrices-from-a-big-matrix#comment_780085

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/497661-how-to-extract-sub-matrices-from-a-big-matrix#comment_780256

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/497661-how-to-extract-sub-matrices-from-a-big-matrix#comment_780256

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/497661-how-to-extract-sub-matrices-from-a-big-matrix#comment_780267

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/497661-how-to-extract-sub-matrices-from-a-big-matrix#comment_780267

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/497661-how-to-extract-sub-matrices-from-a-big-matrix#comment_780271

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/497661-how-to-extract-sub-matrices-from-a-big-matrix#comment_780271

Sign in to comment.