Being more concise with an IF statement

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

 Akzeptierte Antwort

Iain
Iain am 12 Feb. 2014
Bearbeitet: Iain am 12 Feb. 2014

0 Stimmen

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 Kommentare

Tom
Tom am 12 Feb. 2014
Thanks. CN does indeed have just a single column, but your suggestion produces the following error:
Cell contents assignment to a non-cell array object.
Iain
Iain am 12 Feb. 2014
If you caught the code before I edited it to not use CN{4} at the end, you'll get that error.
I'm don't think that's doing what I want. I'm probably being simple though. Is there not some way you could do something like:
if CN(count6,:) == 2:20
CN(2:20)(i,:) = bond_atom;
end
As if there were, I'd understand that better...
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 am 12 Feb. 2014
Thanks! That seems to be the ticket.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Scripts finden Sie in Hilfe-Center und File Exchange

Gefragt:

Tom
am 12 Feb. 2014

Kommentiert:

Tom
am 12 Feb. 2014

Community Treasure Hunt

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

Start Hunting!

Translated by