for loop

5 次查看(过去 30 天)
mahaveer hanuman
mahaveer hanuman 2011-7-23
i have G={[1 0 1;1 0 0;0 0 1;1 1 1] and out put should be I={[1 0;1 0; 0 0;1 1]} how can i get
using for loop
  1 个评论
Fangjun Jiang
Fangjun Jiang 2011-7-23
Not sure if you understand the meaning of {} in MATLAB. It is used to reference cell array. Your example data indicates no need of it. Please do not use it unnecessarily because it might confuse readers regarding your data structure.

请先登录,再进行评论。

采纳的回答

Daniel Shub
Daniel Shub 2011-7-23
If you do not have to use a for loop you can just do:
I = G(:, 1:2)
but if you have to use a for loop:
for ii = 1:size(G, 1)
I(ii, 1:2) = G(ii, 1:2);
end

更多回答(1 个)

Fangjun Jiang
Fangjun Jiang 2011-7-23
Assume the element in I is the first two columns of the element in G.
G=[1 0 1;1 0 0;0 0 1;1 1 1];
[M,N]=size(G);
I=zeros(M,2);
for k=1:size(G,1)
I(k,:)=G(k,1:2);
end
Without for-loop, you can do.
I=G(:,1:2)
  2 个评论
Andrei Bobrov
Andrei Bobrov 2011-7-23
for j1 = 1:size(I,2)
I(:,j1) = G(:,j1);
end
Daniel Shub
Daniel Shub 2011-7-23
and I am giving a +1 to Fanqjun since he (assuming he) gave the same answer, and typed faster than me.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by