chinkare_16
chinkare_16

Reputation: 135

Concatenate matrices with different start index and different end index (Aligning)

for i = 1 : numel(T);
    j = 1 : numel(T(i).n);
P(i,j) = (T(i).n);
G(i) = (T(i).lastPulse)-1100;
Y = P(1,G(1):length(T(1).n));
S = P(2,G(2):length(T(2).n));
end

I have the preceeding code. P is a (191x10000) matrix. I want to take out a specific portion of each row as I showed in S and Y and then concatenate S and Y and other row matrices corresponding to other rows of P to create matrix A(191x[max length of (S,Y,...)]). BUT the tricky part is that I cannot make S and Y aligned.

EXAMPLE:

P = [1 2 1 3 1 1 1 0 3 1 0]
    [3 0 2 0 1 1 4 1 1 2 0];
S = P(1,1:7) = [1 2 1 3 1 1 1];
Y = P(2,5:10) = [1 1 4 1 1 2];
% A = concatenated S and Y aligned to original P. 
A = [ 1   2   1   3   1   1   1  nan nan nan nan]
    [nan nan nan nan  1   1   4   1   1   2  nan];

Preferably I would like to use a loop instead of separated matrices such as S and Y since I have many rows.

Suggested Answer:

I have the idea that probably I have to use indices corresponding to P and use them to concatenate Y and S, I just don't know how to execute this thought especially in a loop.

Upvotes: 0

Views: 74

Answers (1)

Divakar
Divakar

Reputation: 221514

If I got the question correctly in my head, it seems bsxfun could be used here for creating a mask and then keep the masked elements from P and thus have an aligned output. Here's an implementation to go along those lines -

%// Random input array
P = randi(9,5,11)

%// Define the start and stop(end) indices as vectors
start_idx = [1 5 3 4 11]
stop_idx = [7 10 3 6 11]

%// Get the size of P and initialize output array
[M,N] = size(P); 
P_out = NaN(M,N); 

%// Create the mask for extracting specific elements from P
mask = bsxfun(@le,start_idx(:),1:N) & bsxfun(@ge,stop_idx(:),1:N);

%// Put masked elements from P into output array
P_out(mask) = P(mask)

Another way to get the output without initializing it, would be like this -

P_out = P.*mask;
P_out(~mask) = NaN;

So, to correlate with the variables used in the question, start_idx would be G and stop_idx would be [length(T(1).n),length(T(2).n).length(T(3).n),...].

Sample run -

P =
     1     6     8     8     8     1     9     1     2     4     2
     8     8     6     3     7     6     7     2     5     1     2
     6     8     9     5     6     6     6     8     6     5     2
     9     9     5     9     3     7     9     5     1     2     1
     7     1     5     6     6     9     6     8     6     2     6
start_idx =
     1     5     3     4    11
stop_idx =
     7    10     3     6    11
P_out =
     1     6     8     8     8     1     9   NaN   NaN   NaN   NaN
   NaN   NaN   NaN   NaN     7     6     7     2     5     1   NaN
   NaN   NaN     9   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN
   NaN   NaN   NaN     9     3     7   NaN   NaN   NaN   NaN   NaN
   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN   NaN     6

Upvotes: 1

Related Questions