MATLAB: How to index a matrix

matrix index

Hello,
I would like to replace zeros with indexing, ie 1 to 26, and then replace last zero with a -2 please:
clc; clear; %Matrix size columns=10; rows=4; %Blank matrix X = zeros(4,10);
%Fill matrix (1st row & first column) newrow =-ones(1,columns); % the row to replace row 1 with newcolumn=-ones(rows,1); % the column to replace column 1 with
X(1,:)= newrow ; % replace row 1 in a with new
X(:,1) = newcolumn(:) % replace column 1 in a with new
==================================================================== >> X =
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 0 0 0 0 0 0 0 0 0
-1 0 0 0 0 0 0 0 0 0
-1 0 0 0 0 0 0 0 0 0

Best Answer

I am not certain what result you want, so here are two options:
zi = find(X == 0);
X(zi) = [1:26 -2]'; % Option #1
X(zi) = reshape([1:26 -2], [], 3)'; % Option #2
EDIT —
Option #1:
X =
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 1 4 7 10 13 16 19 22 25
-1 2 5 8 11 14 17 20 23 26
-1 3 6 9 12 15 18 21 24 -2
Option #2:
X =
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1
-1 1 2 3 4 5 6 7 8 9
-1 10 11 12 13 14 15 16 17 18
-1 19 20 21 22 23 24 25 26 -2