FOR LOOP WITH CELLS: Index exceeds the number of array elements. Index must not exceed 3.
2 views (last 30 days)
Show older comments
Dearest,
I am unable to solve this error.
I have to compute the mean of voxel, of three lesions(nameLesion_list), in six different "brain maps"(nameMaps_list).
When I run the for loop my command window shows: "Index exceeds the number of array elements. Index must not exceed 3".
I understand that is for the nameLesion_list that is array of three elements but I don't know how I can operate differently with it.
Thank you for your help.
nameLesion_list={lesion_mask_TP0,lesion_mask_TP1,lesion_mask_TP2};
nameMaps_list={mtr_TP0,mtr_TP1,mtr_TP2,mtsat_TP0,mtsat_TP1,mtsat_TP2};
maps_list={mtr_T0_labels,mtr_T1_labels,mtr_T2_labels,mtsat_T0_labels,mtsat_T1_labels,mtsat_T2_labels};
for i_maps=1:length(maps_list)
temp_map=maps_list{i_maps};
temp_nameMap=nameMaps_list{i_maps};
temp_nameLesion=nameLesion_list{i_maps};
[mean_map_lesion,std_map_lesion]=mean_std_map_x_ROI_accorpate(temp_nameMap,temp_nameLesion,ROI_labels.CellLabelNumber);
T_mean_maps.(['mean_' temp_map])=mean_map_lesion(:);
T_std_maps.(['std_' temp_map])=std_map_lesion(:);
end
0 Comments
Accepted Answer
LeoAiE
on 23 Apr 2023
The issue occurs because you are using the same index i_maps to access both nameMaps_list and nameLesion_list. Since nameMaps_list has six elements and nameLesion_list has only three, the index exceeds the number of elements in nameLesion_list.
To fix this, you can use the modulo operator to loop through the nameLesion_list. Here's the corrected code:
nameLesion_list = {lesion_mask_TP0, lesion_mask_TP1, lesion_mask_TP2};
nameMaps_list = {mtr_TP0, mtr_TP1, mtr_TP2, mtsat_TP0, mtsat_TP1, mtsat_TP2};
maps_list = {mtr_T0_labels, mtr_T1_labels, mtr_T2_labels, mtsat_T0_labels, mtsat_T1_labels, mtsat_T2_labels};
for i_maps = 1:length(maps_list)
temp_map = maps_list{i_maps};
temp_nameMap = nameMaps_list{i_maps};
% Use modulo operator to loop through the nameLesion_list
temp_nameLesion = nameLesion_list{mod(i_maps - 1, length(nameLesion_list)) + 1};
[mean_map_lesion, std_map_lesion] = mean_std_map_x_ROI_accorpate(temp_nameMap, temp_nameLesion, ROI_labels.CellLabelNumber);
T_mean_maps.(['mean_' temp_map]) = mean_map_lesion(:);
T_std_maps.(['std_' temp_map]) = std_map_lesion(:);
end
By using the modulo operator, the index will loop through the nameLesion_list while iterating through the nameMaps_list. This way, the index will not exceed the number of array elements in nameLesion_list.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!