フィルターのクリア

Matrix dimensions must agree problem

3 ビュー (過去 30 日間)
Gokhan Kayan
Gokhan Kayan 2018 年 2 月 5 日
コメント済み: Gokhan Kayan 2018 年 2 月 5 日
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.

採用された回答

Guillaume
Guillaume 2018 年 2 月 5 日
You use strcmp to compare char arrays not ==.
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';
%...
However, an even simpler method is to use ismember:
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))
  1 件のコメント
Gokhan Kayan
Gokhan Kayan 2018 年 2 月 5 日
Thank you so much Guillaume :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by