for loop
조회 수: 5 (최근 30일)
이전 댓글 표시
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
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
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
댓글 수: 0
추가 답변 (1개)
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
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!