Question
I wrote a MATLAB code which will put a black box of 10 pixels width at the center of image but I am getting error which says "Index in position 1 exceeds array bounds error" Here is the code for same image = imread('image.jpg'); [r,c]=size(image); width=10; color=6; new_image=ones(r,c)*color; new_image=uint8(new_image); for row=1:r for column=1:(c-width)/2 new_image(row,column)=image(row,column); end; for column=(c+width)/2:c new_image(row,column)=image(row,column); end; end; for column=1:c for row=1:(r-width)/2 new_image(row,column)=image(row,column); end; for row=(r+width)/2:c new_image(row,column)=image(row,column); end; end; imshow(new_image); And for the logic of the code this code makes a matrix of ones as the size of image pixels and after that I iterate the code from the matrix and adding the pixels value of original image to the new matrix where the box is not present.
Expert Answer
Prashant Kumar
PhD Expert
Answered Aug 20, 2026
A better approach is to use indexing, eg.:
>> I = imread('peppers.webp');
>> N = 10;
>> V = 1:N;
>> I(fix((end-N)/2)+V,fix((end-N)/2)+V,:) = 6;
>> imshow(I)

100% Run Guarantee
3-Hour Fast-Track Delivery
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here
Related matlab Questions & Solutions
Browse All →
Explore similar technical troubleshooting questions and verified MATLAB solutions:
Ready-to-Run MATLAB & Simulink Projects
Browse All Projects →