How can i changethe value at a position in an array

1 回表示 (過去 30 日間)
Deniz Bozdogan
Deniz Bozdogan 2021 年 6 月 20 日
回答済み: Star Strider 2021 年 6 月 20 日
Hello, i wanted to cahnge some values in an array but could not do it, can you help me? Thanks in advance.
k=zeros(201);
k(0)=0;
k(101)=1;
k(201)=2;

採用された回答

Star Strider
Star Strider 2021 年 6 月 20 日
The ‘k’ array is a (201x201) array of zeros. The code likely works correctly, however using a single index into an array uses linear indexing. To understand the row and column assignments for element 101 and 201, use the ind2sub function:
k = zeros(201)
k = 201×201
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
k(101)=1;
[r1,c1] = ind2sub(size(k), 101)
r1 = 101
c1 = 1
k(r1,c1)
ans = 1
k(201)=2;
[r2,c2] = ind2sub(size(k), 201)
r2 = 201
c2 = 1
k(r2,c2)
ans = 2
See the documentation on Matrix Indexing for details. .

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by