フィルターのクリア

How to index a matrix

3 ビュー (過去 30 日間)
Aswas
Aswas 2016 年 5 月 12 日
編集済み: Star Strider 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 Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by