필터 지우기
필터 지우기

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 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