Index in position 1 is invalid. Array indices must be positive integers or logical values

2 ビュー (過去 30 日間)
julia zhang
julia zhang 2023 年 2 月 7 日
回答済み: Sulaymon Eshkabilov 2023 年 2 月 7 日
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
  1 件のコメント
julia zhang
julia zhang 2023 年 2 月 7 日
I wanted to add that I am trying to write an m file that creates ands embeds a message into an image.

サインインしてコメントする。

回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023 年 2 月 7 日
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;

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by