Could someone explain below code

16 ビュー (過去 30 日間)
Levent
Levent 2012 年 9 月 13 日
I have code like following
for i = 1:rce(2)
for j = 1:rce(1)
if i == 1 & j == 1
mnn(jj,1:4) = [1 rce(1)+2 rce(1)+3 2];
jj = jj + 1;
elseif i ~= 1 & j == 1
mnn(jj,:) = mnn(jj-1,1:4) + 2;
jj = jj + 1;
end
if j > 1
mnn(jj,:) = mnn(jj-1,1:4) + 1;
jj = jj + 1;
end
end
end
Could someone explain below part for me? What is it for
mnn(jj,:) = mnn(jj-1,1:4) + 2;
and
mnn(jj,:) = mnn(jj-1,1:4) + 1;
Best regards

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 13 日
% just trie these to understand
A=[1 2 3;4 5 6;7 8 9]
A(1:2,:)
% 1:2 means line 1 to line 2 ,
% : means all columns
A(:,2:3) %means all lines , and column 2 to column 3

Wayne King
Wayne King 2012 年 9 月 13 日
編集済み: Wayne King 2012 年 9 月 13 日
Without more context it's hard to say exactly what it's for, but it is simply replacing the jj-th row of mnn with the jj-1 row and adding 2 to each element.
jj must be at least 2 and I'm not sure why they used 1:4 on the RHS because mnn must have only 4 columns.
mnn = randn(4,4);
jj = 2;
mnn(jj,:) = mnn(jj-1,1:4)+2;
You could have just written:
mnn(jj,:) = mnn(jj-1,:)+2;
You should see that the 2nd row is simply the first row with 2 added to each element of the row vector.
The last line simply adds 1.

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

タグが未入力です。

製品

Community Treasure Hunt

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

Start Hunting!

Translated by