Could someone explain below code

13 次查看(过去 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 CenterFile Exchange 中查找有关 Logical 的更多信息

标签

尚未输入任何标签。

产品

Community Treasure Hunt

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

Start Hunting!

Translated by