I wanted to add that I am trying to write an m file that creates ands embeds a message into an image.
Index in position 1 is invalid. Array indices must be positive integers or logical values
2 views (last 30 days)
Show older comments
Hi everyone,
I am not sure why I am getting this error since I feel like my indices are all positive integers, however, I am still getting this error😅 hanks so much in advance.
Here is my code:
The error is showing for the line
cover(currentRow,currentColumn)= payload(K); % this is the line that throws the error
% create and embed message - - - - - - - - - - - - - - - - -
coverorig = imread('turtle.png');
% infooriginal = imfinfo('turtle.png'); % gets information about this file
cover1 = imresize(coverorig, [1024 1024]);
cover = rgb2gray(cover1);
[M,N] = size(cover); %rows and cols in cover
imwrite(cover1, 'turtle.png');
imwrite(cover, 'grayscale_turtle.png');
capacity = ceil(log2(M*N)); % max length of payload can embed, 2^capacity
rate = 0.1;
K = round(rate*(M*N - capacity)); % length of payload to create and embed
payload = round(rand(1,K)); % create random vector of 0s and 1s of correct length
currentColumn = N;
currentRow = M;
currentPayloadIndex = K;
for currentRow = 1:1:1e6
for currentColumn = 1:1:1e6
cover(currentRow,currentColumn)= payload(K);
K = K - 1;
currentColumn = currentColumn - 1;
if K == 1
fprintf('message fully embedded')
end
if K == 1
fprintf('message fully embedded')
end
currentColumn = N;
currentRow = currentRow - 1;
end
end
Answers (1)
Sulaymon Eshkabilov
on 7 Feb 2023
There are a couple of inconsistencies that cause this problem in the code:
%
currentColumn = currentColumn - 1; % Return to be "0" in the next iteration
currentRow = currentRow - 1; % The same
% These declared variables above are never used in the loop:
currentColumn = N;
currentRow = M;
0 Comments
See Also
Categories
Find more on Logical 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!