Matrix index is out of range for deletion.
조회 수: 5 (최근 30일)
이전 댓글 표시
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);
댓글 수: 0
채택된 답변
Steven Lord
2022년 11월 16일
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
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
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
fprintf("Original x\n")
disp(x)
x(todelete) = [];
fprintf("x after deletion\n")
disp(x)
댓글 수: 4
Steven Lord
2022년 11월 16일
@Stephen23 is correct. This is a case where instead of a list of columns to delete you have a list of columns to keep.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!