How to index a matrix
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Star Strider
on 12 May 2016
Edited: Star Strider
on 12 May 2016
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
0 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing 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!