Home > NoiseTools > nt_trial2mat.m

# nt_trial2mat

## PURPOSE

[y,w]=nt_trial2mat(x,max_nsamples) - convert trial cell array to 3D matrix

## SYNOPSIS

function [y,w]=nt_trial2mat(x,max_nsamples)

## DESCRIPTION

    [y,w]=nt_trial2mat(x,max_nsamples) - convert trial cell array to 3D matrix

      y: matrix (samples * channels * trials)
      w: weights (samples * 1 * trials)

      x: trial array (each trial is channels * samples)
      max_nsamples: max number of samples per trial

     In the case of variable-length trials, the weights can be used to indicate
     which parts of the array are valid data.

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

    0001 function [y,w]=nt_trial2mat(x,max_nsamples)
    0002 %[y,w]=nt_trial2mat(x,max_nsamples) - convert trial cell array to 3D matrix
    0003 %
    0004 %  y: matrix (samples * channels * trials)
    0005 %  w: weights (samples * 1 * trials)
    0006 %
    0007 %  x: trial array (each trial is channels * samples)
    0008 %  max_nsamples: max number of samples per trial
    0009 %
    0010 % In the case of variable-length trials, the weights can be used to indicate
    0011 % which parts of the array are valid data.
    0012
    0013 if nargin<2; max_nsamples=[]; end
    0014
    0015 ntrials=numel(x);
    0016
    0017 % determine maximum trial size
    0018 nsamples=0;
    0019 for k=1:ntrials
    0020     [m,n]=size(x{k});
    0021     nsamples=max(n,nsamples);
    0022 end
    0023
    0024 % clip if too large
    0025 if ~isempty(max_nsamples)
    0026     nsamples=min(nsamples,max_nsamples);
    0027 end
    0028
    0029 % load data into 3D matrix
    0030 nchans=m;
    0031 if isa(x{1},'single')
    0032     y=zeros(nsamples, nchans, ntrials, 'single');
    0033 else
    0034     y=zeros(nsamples, nchans, ntrials);
    0035 end
    0036
    0037 %w=zeros(nsamples, 1, ntrials);
    0038 for k=1:ntrials
    0039     [m,n]=size(x{k});
    0040     if n>nsamples;
    0041         % longer than max: clip
    0042         x{k}=x{k}(:,1:nsamples);
    0043         n=nsamples;
    0044     end
    0045     y(1:n,:,k)=x{k}';
    0046     %w(1:n,1,k)=1;
    0047 end
    0048