Being more concise with an IF statement

조회 수: 1 (최근 30일)
Tom
Tom 2014년 2월 12일
댓글: Tom 2014년 2월 12일
Hello,
Could someone tell me if there's a more concise way (I'm sure there is) of implementing the following:
if CN(count6,:) == 2
CN2(i,:) = bond_atom;
CN2(all(CN2==0,2),:)=[];
elseif CN(count6,:) == 3
CN3(i,:) = bond_atom;
CN3(all(CN3==0,2),:)=[];
elseif CN(count6,:) == 4
CN4(i,:) = bond_atom;
CN4(all(CN4==0,2),:)=[];
As you can see, I am creating matrices (CN2, CN3, CN4 etc...) using an IF criterion. However the criterion could be anything from 2 to ~20. So I wonder if I can summarise all the if's and elseifs in just a few lines, instead of having to repeat myself as I am doing here.
Kind regards,
Tom

채택된 답변

Iain
Iain 2014년 2월 12일
편집: Iain 2014년 2월 12일
I'm assuming that CN only has one column.
number = CN(count6);
temp(1,:) = bond_atom;
temp(all(temp==0,2),:) = [];
CNoutput{number} = temp;
Replace CN4 with CNoutput{4}, etc.
  댓글 수: 5
Iain
Iain 2014년 2월 12일
You're right tehre is a subtlety that I'd assumed out of existence.
number = CN(count6);
CNoutput{number}(i,:) = bond_atom;
CNoutput{number}(all(CNoutput{number}==0,2),:) = [];
Tom
Tom 2014년 2월 12일
Thanks! That seems to be the ticket.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by