- it creates a cell array of strings, AmpLimitField
- it takes a cell array, VDIV, as input
Assigning a single value to cell array with logical indexing.
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi All,
I am wondering if the code I have below is optimal for what it does, or if there are better ways to do this.
VDIV{1} = [2 5 7 10 13 13.2 20 30 50 100 200 500 1000]*1E-3;
VDIV{2} = [VDIV{1} 5 10];
numVDIVs = numel(VDIV{2});
AmpLimitField = cell(numVDIVs,1);
[AmpLimitField{VDIV{2} < 102E-3}] = deal('X1');
[AmpLimitField{VDIV{2} >= 1.02}] = deal('X100');
% When VDIV{2} >= 102E-3 && VDIV{2} < 1.02}
[AmpLimitField{cellfun(@isempty,AmpLimitField)}] = deal('X10');
Outputs:
AmpLimitField =
'X1'
'X1'
'X1'
'X1'
'X1'
'X1'
'X1'
'X1'
'X1'
'X1'
'X10'
'X10'
'X10'
'X100'
'X100'
0 Kommentare
Akzeptierte Antwort
per isakson
am 11 Mai 2013
Bearbeitet: per isakson
am 11 Mai 2013
"... for what it does ..."
However, I see no reason why not use an array of double in place of VDIV. Thus, I have replaced VDIV{2} by vdv.
Your assignment
[AmpLimitField{cellfun(@isempty,AmpLimitField)}] = deal('X10');
is questionable, since it may hide mistakes in the logic. Whether or not all the greater and lesser than are correct a string will be assigned to AmpLimitField. It is better that a mistake show up as an empty cell.
One more comment
CellArray( condition ) = { string };
is easier to read and a bit faster than
[ CellArray{ condition } ] = deal( string );
.
Try
>> cssm
Elapsed time is 0.000082 seconds.
Elapsed time is 0.000228 seconds.
Equal!
>>
where
function cssm()
tic
alf1 = cssm_();
toc
tic
alf2 = op();
toc
if all( cellfun( @(s1,s2) all(eq(s1,s2)), alf1, alf2 ) )
disp( 'Equal!' )
else
disp( 'Not equal!' )
end
end
function alf = cssm_()
vdv = [ [2,5,7,10,13,13.2,20,30,50,100,200,500,1000]*1E-3, 5, 10 ];
len = numel( vdv );
alf = cell( len, 1 );
alf( vdv < 0.102 ) = {'X1'};
alf( vdv >= 0.102 & vdv < 1.02 ) = {'X10'};
alf( vdv >= 1.02 ) = {'X100'};
end
function AmpLimitField = op()
VDIV{1} = [2 5 7 10 13 13.2 20 30 50 100 200 500 1000]*1E-3;
VDIV{2} = [VDIV{1} 5 10];
numVDIVs = numel(VDIV{2});
AmpLimitField = cell(numVDIVs,1);
[AmpLimitField{VDIV{2} < 102E-3}] = deal('X1');
[AmpLimitField{VDIV{2} >= 1.02}] = deal('X100');
% When VDIV{2} >= 102E-3 && VDIV{2} < 1.02}
[AmpLimitField{cellfun(@isempty,AmpLimitField)}] = deal('X10');
end
1 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!