Need help with a for loop implementation for the below code

2 次查看(过去 30 天)
Greetings! I'm currently working on a project where in I perform a number of operations on a stimulus file (fancy name for a .mat file). The file consists of multiple trials and as of now, the following code is able to successfully carry out the process one trial at a time. I tried automating this code for all trials with the following but throws out this particular error:
N-dimensional indexing allowed for full matrices only
Can anyone help me debug the code. It's been bugging (!) me all day and I'm still not sure where I'm going wrong. Here's the code for reference:
function map = mkSaliencyMap(d,trialIds,varargin)
binsz = 0.1;
sdev = 2.0;
dt = 0.020;
for itrial = (trialIds)
if nargin < 3
trialIds = 1:d.numTrials;
end
eyedata = d.meta.image.filename('time',Inf).data;
eye = d.eye(trialIds(itrial));
t0 = d.meta.cic.firstFrame('trial',trialIds(itrial));
% Determining the start and end times at which the image is displayed
[tstarttime,tr,frame,dta] = d.meta.image.startTime('trial',trialIds(itrial));
[tstoptime, tr, frame, dta] = d.meta.image.stopTime('trial',trialIds(itrial));
% Removing eyetraces before and after the image is displayed
blink = false(size(eye.t));
blink = blink | eye.t < tstarttime-t0;
blink = blink | eye.t > tstoptime-t0;
% Defining deformities in the plot as a result of blinks
[tstart,tr,frame,dta] = d.meta.edf.STARTBLINK('trial',trialIds(itrial));
tend = d.meta.edf.ENDBLINK('trial',trialIds(itrial));
% Iterating over each blink datapoint
tstart = tstart - t0 - dt
tend = tend - t0 + dt
if max(tend) < max(tstart)
tend = [tend, Inf];
end
if min(tstart) > min(tend) % must be missing a start event
tstart = [-Inf, tstart];
end
assert(numel(tstart) == numel(tend),'STARTBLINK/ENDBLINK event mismatch!');
for itrial = 1:length(tstart)
blink = blink | eye.t > tstart(itrial) & eye.t < tend(itrial);
end
% Defining the original dimensions of the image
width = d.meta.image.width('trial', trialIds(itrial), 'time', Inf).data
height = d.meta.image.height('trial', trialIds(itrial), 'time', Inf).data
sz = ceil([height, width]./binsz); % size of the saliency map
x0 = floor(sz(2)/2)+1;
y0 = floor(sz(1)/2)+1; % Co-ordinates of the screen centre
% Removing eyetraces which are negative integers
blink = blink | (ceil(eye.x/binsz)+x0 <= 0) | (ceil(eye.x/binsz)+x0 >= sz(2));
blink = blink | (ceil(eye.y/binsz)+y0 <= 0) | (ceil(eye.y/binsz)+y0 >= sz(1));
% Assigning NaN values to all blink datapoints for trial 1
eye.x(blink) = NaN;
eye.y(blink) = NaN;
% Creating a sparse matrix of the eyetraces with the blinks removed
map(:,:,itrial) = sparse(ceil(eye.y(~blink)/binsz)+y0,ceil(eye.x(~blink)/binsz)+x0,1,sz(1),sz(2));
map(:,:,itrial) = full(map(:,:,itrial));
map(:,:,itrial) = imgaussfilt(map(:,:,itrial),sdev); % Gaussian filter with stdev = 2
end
end
  3 个评论
Kevin Akash Rajasekaran
The last part part of the code dealing with the sparse matrix maps i.e
map(:,:,itrial) = sparse(ceil(eye.y(~blink)/binsz)+y0,ceil(eye.x(~blink)/binsz)+x0,1,sz(1),sz(2));
Walter Roberson
Walter Roberson 2021-4-16
What class() are the inputs?
At the time of the error, what are size() of each of the variables mentioned on the line ?

请先登录,再进行评论。

回答(2 个)

Jan
Jan 2021-4-16
Multi-dim sparse arrays are not implemented in Matlab. Try to omit the sparse() .

Walter Roberson
Walter Roberson 2021-4-16
map(:,:,itrial) = sparse(ceil(eye.y(~blink)/binsz)+y0,ceil(eye.x(~blink)/binsz)+x0,1,sz(1),sz(2));
The right hand side appears to be a normal sparse array. However, sparse arrays cannot be assigned to a three dimensional array. For example
>> map(:,:,1) = sparse(1,1,1)
N-dimensional indexing allowed for full matrices only.
You will need to recode, such as
map(:,:,itrial) = full(sparse(ceil(eye.y(~blink)/binsz)+y0,ceil(eye.x(~blink)/binsz)+x0,1,sz(1),sz(2)));
in which case you can skip the next line
map(:,:,itrial) = full(map(:,:,itrial)); %no longer needed
You cannot mix sparse and full in the same array, by the way.

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by