How to solve matrix dimension must agree error in the following program.

2 ビュー (過去 30 日間)
PRASHIK GAIKWAD
PRASHIK GAIKWAD 2018 年 9 月 28 日
編集済み: Basil C. 2018 年 9 月 28 日
d = 100;
r = mod(a,b);
for a = 2:1:d
c = 0;
for b = 1:1:a
if r == 0
c = c+1;
end
end
if c == 2
fprintf('value of a = %d',b,c,d)
end
end
  3 件のコメント
PRASHIK GAIKWAD
PRASHIK GAIKWAD 2018 年 9 月 28 日
d = 100 and a = 2:1:d and b = 1:1:a c = 0 and if a/b = 0,then c = c+1,when c=2 then print value of a. That's the program I am trying to run.But I am getting an error saying Matrix dimension must agree
madhan ravi
madhan ravi 2018 年 9 月 28 日
you have defined a and b after using those variables?
>> d = 100;
r = mod(a,b);
for a = 2:1:d
c = 0;
for b = 1:1:a
if r == 0
c = c+1;
end
end
if c == 2
fprintf('value of a = %d',b,c,d)
end
end
Undefined function or variable 'b'.

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

採用された回答

Basil C.
Basil C. 2018 年 9 月 28 日
編集済み: Basil C. 2018 年 9 月 28 日
Hi, according to what you stated in the comment there are a few mistakes i would like to points out
1. when b=1:1:a you only limit the value of b to be equal to [1,2] as it considers a to be a(1)
2. mod(a,b) requires the size of a and b to be the same which is clearly not since a has a size of 99 while b has only 2 elelments
Guessing from the code it seems like you are trying to write a code for generating prime numbers. Then I feel this is what you are looking for
d = 100;
a = 2:1:d ;
c = 0 ;
for a = 2:1:d
c = 0;
for b = 1:1:a
r = mod(a,b);
if r == 0
c = c+1;
end
end
if c == 2
disp(['The value of a is = ',num2str(b)]);
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeNumeric Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by