Trying to create a vector in steps of 0.5
95 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
eoin247
am 24 Jan. 2016
Kommentiert: Stephen23
am 9 Jun. 2023
I'm trying to get the vector to go up in steps of 0.5.
v_numerical(0.5)= g.*0.5;
for i = 1:0.5:10
v_numerical(i) = v_numerical(i-0.5)+(g-((c.*v_numerical(i-0.5))./m).*(0.5));
end
However whenever i try running it I get the error:
Attempted to access v_numerical(0.5); index must be a positive integer or logical.
Any idea how I can solve this problem? Many thanks in advance!
0 Kommentare
Akzeptierte Antwort
Stephen23
am 24 Jan. 2016
Bearbeitet: Stephen23
am 24 Jan. 2016
MATLAB indices start at one, and must be whole numbers (i.e. no fraction allowed). With your code you are trying to index with 0.5 and lots of other fractions, which will always be an error, like this:
>> A(0.5) = 3 % fraction index is an error
??? Attempted to access (0.5); index must be a positive integer or logical.
>> A(1) = 3 % whole number index is okay
A =
3
You can easily create a vector with 0.5 spacing:
V = 1:0.5:10;
but when I tried to fix your code it made very little sense to me what you are trying to achieve. Can you please explain what you are trying to do with this code: what should the input and output be?
2 Kommentare
Weitere Antworten (1)
Nemanja
am 9 Jun. 2023
Bearbeitet: Nemanja
am 9 Jun. 2023
g = linspace(1:10:20) %goes from 1 to 10 and has 20 elements because 10/20 is 0.5 so it will go up from 1 to 10 with 0.5 spacing%
1 Kommentar
Stephen23
am 9 Jun. 2023
"goes from 1 to 10 and has 20 elements because 10/20 is 0.5 so it will go up from 1 to 10 with 0.5 spacing"
No, you are making this error:
It is easy to check that your statement is incorrect: did you try running the code yourself before posting here?
g = linspace(1,10,20) % fixed incorrect syntax
Tip: to split the range 1..10 into steps of 0.5 then you need 19 elements. Lets try it now:
g = linspace(1,10,19)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!