Matrix dimensions must agree problem
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have an cell array (7879*1) and the first 6 six terms are:
LV.cc-LV.vr.cr
CL.ptp-LV.cr.ca
LP.li-CL.ptp
LV.cc-LV.vr.cr
CL.ptp-LV.cr.ca
LP.li.ca-RG.le.ca
SC.gl
CM.vr-RG.ca
I want to write a code that if cell is equal to LV.cc-LV.vr.cr then B is equal to A, and if cell is equal to SC.gl then B is equal to E. I write this code but it gives me error "Matrix dimensions must agree problem". My code is:
for i=1:numel(cell);
if cell{i}=='LV.cc-LV.vr.cr';
B{i}='A';
else if cell{i}=='CL.ptp-LV.cr.ca';
B{i}='B';
else if cell{i}=='LP.li-CL.ptp';
B{i}='C';
else if cell{i}=='LP.li.ca-RG.le.ca';
B{i}='D';
else if cell{i}=='SC.gl';
B{i}='E';
else cell{i}=='CM.vr-RG.ca';
B{i}='F';
How can I solve Matrix dimensions must agree problem ? Thank you.
0 commentaires
Réponse acceptée
Guillaume
le 5 Fév 2018
Also note that there is a big difference between elseif and else if. The former continues the previous if, the latter starts a new if within the else portion of the previous if requiring an additional end.
Also, note that calling your cell array cell is a very bad idea, since you won't be able to create other cell arrays with cell function, now shadowed by your variable
So:
if strcmp(carray{i}, 'LV.cc-LV.vr.cr')
B{i}='A';
elseif strcmp(carray{i}, 'CL.ptp-LV.cr.ca')
B{i}='B';
%...
B = cell(size(carray)); %using the cell function which can't be used if your cell array is called cell!
replacements = {'A', 'B', 'C', 'D', 'E', 'F'};
[found, where] = ismember(carray, {'LV.cc-LV.vr.cr', 'CL.ptp-LV.cr.ca', 'LP.li-CL.ptp', 'LP.li.ca-RG.le.ca', 'SC.gl', 'CM.vr-RG.ca'});
B(found) = replacements(where(found))
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!