generating an equispaced vector using a variable

Hi, I have a variable that I want to use to make each variable in part of a vector equi-spaced, and I cannot figure out why it is not working. In the code below it works for variable "a" but not "aNumber", I'm sure this is obvious but I cannot see it. Does anyone know why Matlab is treating "a" differently from "aNumber"?
B=nan(57,5)
aNumber=1.111
a=1
B(1:50,1)=(1:50)
B(1:50,2)=(1:1:50)
B(1:50,3)=(a:1:a*50)
B(1:50,4)=(a:a*50)
B(1:50,5)=(aNumber : aNumber*50)
gives...
Subscripted assignment dimension mismatch.
Error in untitled36 (line 25)
B(1:50,5)=(aNumber : aNumber*50)
Best regards,
Steve

5 件のコメント

Stephen23
Stephen23 2018 年 3 月 15 日
編集済み: Stephen23 2018 年 3 月 15 日
"Does anyone know why Matlab is treating "a" differently from "aNumber"?"
You do not say what value aNumber has, but I am guess that it is not equal to one: if this is the case, how many elements does this have?:
aNumber : aNumber*50
Lets try an example, with aNumber = 2:
2 : 2*50
2 : 100
Does this have 50 elements, like the first four rows have? How did you check?
Guillaume
Guillaume 2018 年 3 月 15 日
@Stephen, he does say (but the post was badly formatted). aNumber is 1.111
Stephen Devlin
Stephen Devlin 2018 年 3 月 15 日
For some insane reason I just thought aNumber:aNumber*50 would give 50 multiples of aNumber, much appreciated assistance from everybody, apologies for overlooking the formatting which didn't include aNumber as code.
Guillaume
Guillaume 2018 年 3 月 15 日
aNumber * (1:50)
would give the 50 multiples
Stephen Devlin
Stephen Devlin 2018 年 3 月 15 日
Thank you Guillaume

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

 採用された回答

KSSV
KSSV 2018 年 3 月 15 日

1 投票

This would be more apt:
B(1:50,5)=linspace(aNumber , aNumber*50,50)
then
B(1:50,5)=(aNumber : aNumber*50)
Your line creates 1X55 vector, so the error...you actually need 1x50 vector.

3 件のコメント

Stephen Devlin
Stephen Devlin 2018 年 3 月 15 日
hi KSSV, how is it creating a 1x55 vector? Ive checked your answer and it works, I still can't see why mine didn't for "aNumber" when the same code worked for "a"?
Guillaume
Guillaume 2018 年 3 月 15 日
編集済み: Guillaume 2018 年 3 月 15 日
how is it creating a 1x55 vector
>> aNumber = 1.111
>> aNumber*50
ans =
55.549999999999997
>> aNumber : aNumber*50
ans =
Columns 1 through 9
1.111 2.111 3.111 4.111 5.111 6.111 7.111 8.111 9.111
...
Column 55
55.111
Stephen Devlin
Stephen Devlin 2018 年 3 月 15 日
(stabs head with fork) I get it now, thanks KSSV

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

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by