# Index in position 3 exceeds array bounds. Index must not exceed 1.

9 views (last 30 days)
Oguzhan Tasyaran on 19 Nov 2021
Answered: Cris LaPierre on 19 Nov 2021
clc
clear all
close all
warning off
r1=im1(:,:,1);
g1=im1(:,:,2);
b1=im1(:,:,3);
r2=im2(:,:,1);
g2=im2(:,:,2);
b2=im2(:,:,3);
a=myown(r1,r2);
b=myown(g1,g2);
c=myown(b1,b2);
nexttile;
imshow(im1);
nexttile;
imshow(im2);
d=cat(3,a,b,c);
nexttile
imshow(d);
myown
function mattu=myown(p,q)
M = zeros(256,1,'uint8');
hist1 = imhist(p);
hist2 = imhist(q);
cdf1 = cumsum(hist1) / numel(p);
cdf2 = cumsum(hist2) / numel(q);
for idx = 1 : 256
[~,ind] = min(abs(cdf1(idx) - cdf2));
M(idx) = ind-1;
end
[H, W] = size(p);
mattu=zeros(H,W,'uint8');
for x = 1: H
for y = 1:W
mattu(x,y) =M(double(p(x,y))+1);
end
end
end
Index in position 3 exceeds array bounds. Index must not exceed 1. I get an error
Jan on 19 Nov 2021
warning off
This is an extremely bad idea. Warnings are the best friend of programmers. Do not switch them off.
Please post the complete error message. Then we do not have to guess, here the error occurs.

Cris LaPierre on 19 Nov 2021
You have a variable that is an mxnx1 array, and your code is telling MATLAB to index the 3rd dimension with a value >1.
theta1 = rand(3,2,1);
% Works
theta1(1,2,1)
ans = 0.7282
ans = 0.7707
ans = 0.7707
% Doesn't work
theta1(1,2,3)
Index in position 3 exceeds array bounds. Index must not exceed 1.