2 views (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!

Stephen Cobeldick
on 15 Nov 2020

Edited: Stephen Cobeldick
on 15 Nov 2020

Is zero a valid index?

n = 2;

i = 262140;

mod(i+n+1, 262143)

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)

Stephen Cobeldick
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.

VBBV
on 15 Nov 2020

Edited: VBBV
on 15 Nov 2020

%if tru

I = 0:262142; n = 2;

for k = 1: length(I)

z(k) = x(mod(I(k)+n, 262143)); %error: Subscript indices must either be real positive integers or logicals

end

From your snapshot image you can define I as vector and access them like above

Stephen Cobeldick
on 15 Nov 2020

This answer:

- fails to take into account that the provided algorithm uses zero-based indexing.
- still throws an error when the output from mod is zero:

n = 2;

i = 262141;

mod(i+n, 262143)

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/648278-error-when-accessing-indices-of-a-vector#comment_1134348

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/648278-error-when-accessing-indices-of-a-vector#comment_1134348

Sign in to comment.