1

I need to create a 1-D array of 2-D arrays, so that a program can read each 2-D array separately.

I have a large array with 5 columns, with the second column storing 'marker' data. Depending on the marker value, I need to take the corresponding data from the remaining 4 columns and put them into a new array on its own.

I was thinking of having two for loops running, one to take the target data and write it to a cell in the 1-D array, and one to read the initial array line-by-line, looking for the markers.

I feel like this is a fairly simple issue, I'm just having trouble figuring out how to essentially cut and paste certain parts of an array and write them to a new one.

Thanks in advance.

spanigrahi
  • 11
  • 3
  • Can you give example input and output? It's a little hard to understand exactly what you want. – Nathan Henkel Jun 24 '13 at 02:33
  • Sorry about that. For the input I have a large 2D array, with one column providing 'marker' data. I would like to take each block of data associated with a certain marker and create a smaller array of the selected data. Since the marker appears more than once, each set of data should be stored in a separate 2D array. I then need a 1D array that stores each of these 2D arrays. – spanigrahi Jun 24 '13 at 18:05

2 Answers2

1

No for loops needed, use your marker with logical indexing. For example, if your large array is A :

B=A(A(:,2)==marker,[1 3:5]) 

will select all rows where the marker was present, without the 2nd col. Then you can use reshape or the (:) operator to make it 1D, for example

B=B(:)

or, if you want a one-liner:

B=reshape(A(A(:,2)==marker,[1 3:5]),1,[]); 
Community
  • 1
  • 1
bla
  • 25,846
  • 10
  • 70
  • 101
  • +1 for logical indexing. As for the other point, I think a cell array is what the OP is looking for: `C = num2cell(B,2)` (a cell array where each element is a 1x4 vector) – Amro Jun 24 '13 at 03:50
  • Thank you for introducing me to logical indexing. This seems much more efficient than nested loops. From what I understand, and by looking at the output running the code you provided me, the script looks for the marker in the second column of array A, then writes the information to a 1D array B. For my script, however, I need to filter out each time there is a 'block' of data with the assigned marker, and write it into its own cell within the array B, similar to what Amro said I believe. Please look at my comment reply to Nathan Henkel in my original post for an example. – spanigrahi Jun 24 '13 at 17:50
0

I am just answering my own question to show any potential future users the solution I came up with eventually.

%=======SPECIFY CSV INPUT FILE HERE========
MARKER_DATA=csvread('ESphnB2.csv');                        % load data from csv file
%===================================

A=MARKER_DATA(:,2);                                         % create 1D array for markers
A=A';                                                       % make column into row

for i=1:length(A)                                           % for every marker
    if A(i) ~= 231                                          % if it is not 231 then
        A(i)=0;                                             % set value to zero
    end
end

edgeArray = diff([0; (A(:) ~= 0); 0]);                      % set non-zero values to 1
ind = [find(edgeArray > 0) find(edgeArray < 0)-1];          % find indices of 1 and save to array with beginning and end

t=1;                                                        % initialize counter for trials
for j=1:size(ind,1)                                         % for every marked index
    B{t}=MARKER_DATA(ind(j,1):ind(j,2),[3:6]);              % create an array with the rows from the data according to indicies
    t=t+1;                                                  % create a new trial
end

gazeVectors=B';                                             % reorient and rename array of trials for saccade analysis

%======SPECIFY MAT OUTPUT FILE HERE===
save('Trial_Data_2.mat','gazeVectors');                       % save array to mat file
%=====================================
spanigrahi
  • 11
  • 3