# The code works but it gives this warning. How to remove this warning?

2 views (last 30 days)
Sadiq Akbar on 3 Mar 2023
Commented: Sadiq Akbar on 4 Mar 2023
In the attachment, run the "main.m" file. It works but it continuiusly gives the following warning types:
Warning: Integer operands are required for colon operator when used as index.
> In L2_Array (line 14)
In fpa1 (line 36)
In main (line 16)
How to remove these warnings?

Steven Lord on 3 Mar 2023
Looking at the contents of the attached L2_Array1.m:
textOfFile = 10×1 string array
"function r=L2_Array1(N)" "d = 0.5;" "N_on_each_axis = (N-1)/3;" "r = zeros(3*N_on_each_axis+1,3);" "v = (N_on_each_axis*d:-d:d).';" "r(2:N_on_each_axis+1,1) = v;" "r(N_on_each_axis+2:2*N_on_each_axis+1,2) = v;" "r(2*N_on_each_axis+2:3*N_on_each_axis+1,3) = v;" "r;" "end"
If N is not one more than a multiple of 3, N_on_each_axis is not an integer value. When you use it in constructing the indices into r, those indices aren't integer values and will cause that warning to be issued, the same way the code below does.
x = 1:10;
y = x(2.5:2.5:10)
Warning: Integer operands are required for colon operator when used as index.
y = 1×4
3 5 8 10
You probably want to round the indices.
y = x(round(2.5:2.5:10))
y = 1×4
3 5 8 10
Sadiq Akbar on 3 Mar 2023
Thanks a lot for your kind resppnse dear Steven Lord. I replaed N by 7 which is one more than multiple of 3. Likewise I replaced M by 10 which again is one more than multiple of 3. But still it gives the same warnings.

Jan on 3 Mar 2023
You call the function L2_Array in ByAskic.m with the arguments 6 and 10.
Inside L2_Array you calculate
N_on_each_axis = (N-1)/3;
r = zeros(3*N_on_each_axis+1,3);
v = (N_on_each_axis*d:-d:d).';
r(2:N_on_each_axis+1,1) = v;
r(N_on_each_axis+2:2*N_on_each_axis+1,2) = v;
For N=6 and 10, N_on_each_axis is 5/3 and 3. In the first case, this is not an integer and as the error message tells you, this is not valid for indexing. Indices must be positive integers.
Sadiq Akbar on 4 Mar 2023
Thank you very much dear Jan for your kind response. You are right. But when I replaced N=6 by N=7, and ran it, still it gives the same warnings.
Sadiq Akbar on 4 Mar 2023
Sorry it is my mistake. Yes, you both are right. Actually I have saved the ByAsic.m with two different names. So I did changes in ByAskic.m but not in the other and inside the main, I was calling the other which I forgot to replace with ByAskic.m. I want to accept the answers of you both. But due to Mathworks restrictions, I have to accept only one. As Steven Lord was the first who has given me the answer. So, I am going to give vote to Jan and accept the answer of Steven Lord. Thanks to you both once again.

### Categories

Find more on Elementary Math in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!