フィルターのクリア

Matrix dimensions must agree error in FOR loop

1 回表示 (過去 30 日間)
Ali Tawfik
Ali Tawfik 2020 年 1 月 29 日
編集済み: James Tursa 2020 年 1 月 29 日
Hi all,
I am trying to do a conditional statement inside for loop, I got a very strange error of matrix dimensions must agree because I am not working with arrays at all,
the code is the following ( I am using MATLAB 2016a):: ANY HELP PLEASEEEE
for seg_nu=1:input('enter segment numbers: ')
z=input('Is segment parallel to the y axis y/n','s')
if z=='n';
z=1
elseif z=='y';
z=2
else
z=3
end
end
  3 件のコメント
Ali Tawfik
Ali Tawfik 2020 年 1 月 29 日
clear all
close all
clc
for seg_nu=1:input('enter segment numbers: ')
z=input('Is segment parallel to the y axis y/n','s')
if z=='no';
z=1
elseif z=='yes';
z=2
else
z=3
end
end
Ali Tawfik
Ali Tawfik 2020 年 1 月 29 日
line 7
Error using ==
Matrix dimensions must agree.
Error in segment_trial (line 7)
if z=='no';

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

回答 (1 件)

James Tursa
James Tursa 2020 年 1 月 29 日
編集済み: James Tursa 2020 年 1 月 29 日
This comparison:
z == 'no'
compares z to a 1x2 array.
And this comparison
z == 'yes'
compares z to a 1x3 array.
So you are in fact dealing with arrays here. Suppose you had entered 'yes' as the input, then guess what happens:
>> 'yes' == 'no'
Error using ==
Matrix dimensions must agree.
You get an error because you are comparing a 1x3 array to a 1x2 array. To make string comparisons, use string compare functions designed for it. E.g., strcmpi
if strcmpi(z,'no') || strcmpi(z,'n')
etc.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by