How to index a matrix

1 次查看(过去 30 天)
Aswas
Aswas 2016-5-12
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

采纳的回答

Star Strider
Star Strider 2016-5-12
编辑:Star Strider 2016-5-12
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 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by