Matrix index is out of range for deletion.

9 views (last 30 days)
Hello!
I cannot figure out why this is breaking. It should not be out of range as we are just comparing values. Could you please help me get to the bottom of this?
Function Call in main script:
%% Regional Channel Break down:
% each channel
% each sub-region
% included in the function call
R1 = [1,2,3,17,5,18,6,11]; % right ventrolateral prefrontal cortex
R2 = [4, 9, 10]; % right ventrolateral prefrontal cortex
R3 = [7, 8, 21,22, 12, 13,25, 26]; % right frontopolar prefrontal cortex
R4 = [14, 15, 16, 29, 30]; % right oribitofrontal cortex
L1 = [19,20,33,34,35, 38,39,43]; % left ventrolateral prefrontal cortex
L2 = [40, 44, 45]; % left ventrolateral prefrontal cortex
L3 = [23, 24, 27, 28, 36, 37, 41, 42]; % left frontopolar prefrontal cortex
L4 = [31, 32, 46, 47, 48]; % left oribitofrontal cortex
regions = input(['Which Region? Enter one of the following: ' ...
'R1, R2, R3, R4, L1, L2, L3, L4: '])
%Looping through a struct:
epochMatrixRegions = zeros(24,4);
fn=fieldnames(dataStruct); %datastruct contains the timestamps
fn=fn(2:end);
i = 1;
%loop through the fields
for i = 1:numel(fn)
%access the data
stamps=dataStruct.(fn{i});
oxyhb= Participants.(fn{i});
%call function
[epochMatrixRegions(i,1), epochMatrixRegions(i,2),...
epochMatrixRegions(i,3), epochMatrixRegions(i,4)] = ...
thinkAloudAUC_Regions(stamps,oxyhb, regions);
end
Part inside the fcn that is breaking:
%% Regions
R1 = [1,2,3,17,5,18,6,11]; % right ventrolateral prefrontal cortex
R2 = [4, 9, 10]; % right ventrolateral prefrontal cortex
R3 = [7, 8, 21,22, 12, 13,25, 26]; % right frontopolar prefrontal cortex
R4 = [14, 15, 16, 29, 30]; % right oribitofrontal cortex
L1 = [19,20,33,34,35, 38,39,43]; % left ventrolateral prefrontal cortex
L2 = [40, 44, 45]; % left ventrolateral prefrontal cortex
L3 = [23, 24, 27, 28, 36, 37, 41, 42]; % left frontopolar prefrontal cortex
L4 = [31, 32, 46, 47, 48]; % left oribitofrontal cortex
%% Load Participant Data:
% The odd columns are HbO, the even are HbR
% for now, we only want to look at oxygenated data
HbO = oxyhb.data(:,2:2:end);
%filter the HbO data for the desired region:
channels = 1:1:48;
for j = 1:length(channels)
if ismember(j,regions) == false
HbO(:,j) = [];
end
end
The error I get is:
Matrix index is out of range for deletion.
Error in thinkAloudAUC_Regions (line 45)
HbO(:,j) = [];
Error in ThinkAloudProcessing (line 265)
thinkAloudAUC_Regions(stamps,oxyhb, regions);

Accepted Answer

Steven Lord
Steven Lord on 16 Nov 2022
What happens if you try to delete the 11th element of a vector with 10 elements?
try
x = 1:10;
x(11) = [];
catch ME
fprintf("This code threw error '%s'", ME.message)
end
This code threw error 'Matrix index is out of range for deletion.'
What happens if you have a vector with 10 elements, delete one of those elements, then try to delete the 10th element of the result?
try
x = 1:10;
for k = 1:10
if k == 5 | k == 10
fprintf('Deleting element %d of a %d element long vector.\n', k, length(x))
x(k) = [];
end
end
catch ME
fprintf("This code threw error '%s'", ME.message)
end
Deleting element 5 of a 10 element long vector. Deleting element 10 of a 9 element long vector.
This code threw error 'Matrix index is out of range for deletion.'
One common solution to this is to work your way backwards, starting with the last element working your way towards the first. Another solution would be to create a vector of elements to delete or to keep inside the loop and only perform the actual pruning at the end.
x = 1:10;
todelete = false(size(x));
for k = 1:10
if k == 5 | k == 10
fprintf('Deleting element %d of a %d element long vector.\n', k, length(x))
todelete(k) = true;
end
end
Deleting element 5 of a 10 element long vector. Deleting element 10 of a 10 element long vector.
fprintf("Original x\n")
Original x
disp(x)
1 2 3 4 5 6 7 8 9 10
x(todelete) = [];
fprintf("x after deletion\n")
x after deletion
disp(x)
1 2 3 4 6 7 8 9
  4 Comments

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!