Error when accessing indices of a vector

1 view (last 30 days)
Hello!
I am trying to code something in Matlab and it involves a lot of accessing elements in vectors. Below is a snippet of code that I am working on:
x(1)=1;
for i=2:18
x(i)=0;
end
for i=1:18
y(i)=1;
end
for i = 0:262124
x(i+18+1) = x(i+7+1) + mod(x(i+1),2);
y(i+18+1) = y(i+10+1) + y(i+7+1) + y(i+5+1) + mod(y(i+1), 2);
end
% n can be = 0, 1, 2,..., 262142
n = 2;
for i = 0: 262142
z(i+1) = x(mod(i+n+1, 262143)); %error: Subscript indices must either be real positive integers or logicals.
end
In the last "for" loop where I am initialising vector z(), I get an error saying: "Subscript indices must either be real positive integers or logicals." However, when I do not suppres z(i+1) by ommiting the semi colon, the program is able to run, and I can see the values of z in the workspace. Why is this?
The code I am writing in Matlab is based upon the series of instructions shown in the image below. However, I can't seem to track down my error which leads to me not being able to access the elements of x() (without not suppressing the output of z()).
I appreciate any guidance. Thank you!

Accepted Answer

Stephen23
Stephen23 on 15 Nov 2020
Edited: Stephen23 on 15 Nov 2020
Is zero a valid index?
n = 2;
i = 262140;
mod(i+n+1, 262143)
ans = 0
Solution: to adjust from zero-based indexing (shown in the provided algorithm) to one-based indexing (used in MATLAB) you need to add one to mod's output, not its input:
1+mod(i+n, 262143)
ans = 262143
  2 Comments
Mich Elly
Mich Elly on 15 Nov 2020
Hi Mr,
Thank you for your help.
I have one question with regards to this snippet of code I wrote below.
That last term that is being summed is supposed to perform x(i) mod 2, where i is the index that starts from 0. In this instance, is it still incorrect to handle the index offset by incrementing 1 in the mod's input? Becuase without incrementing it in the mod's input in this instance, I would be accessing x(0) in matlab in the for loop.
Thank you for your help :)
for i = 0:262124
x(i+18+1) = x(i+7+1) + mod(x(i+1),2);
Stephen23
Stephen23 on 15 Nov 2020
Edited: Stephen23 on 15 Nov 2020
@Mich Elly: the output of that mod is not being used as an index (unlike the mod referred to in your question and my answer), so you do not need to make any changes there: x(0) in the original algorithm is x(1) in MATLAB. So with the code shown you already refer to the same value and perform the same operation on it.
"In this instance, is it still incorrect to handle the index offset by incrementing 1 in the mod's input"
Unlike in your original question, in this case you are not incrementing by one "in the mod's input". You are actually incrementing by one in the indexing into x. Which is the correct way to access that specifix value. But there is no reason to change whatever operation you are performing on that value.
Different mod, different purpose.

Sign in to comment.

More Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!