List index out of range error while it’s not - range

I am just a beginner
f=open(‘m.txt’)
for lines in f:
if lines.startswith(‘From’):
g=lines.split()
g2=g[5]
print(g2)
The line in the file is like:
From stephen.marquard#uct.ac.za Sat Jan 5 09:14:16 2008(a single line)
It is showing index out of range error on 2nd last line.

When you split using 'From', your code becomes something like this :
g = ['stephen.marquard#uct.ac.za Sat Jan 5 09:14:16 2008']
Since size/length of g is only 1, it will certainly give u an IndexOutOfBounds error if you try to access g[5]
If you want to access the element at 5th index in the above phrase ('e' here), you could do the following:
print(g[0][5])

Related

Lotto code,the previous number cannot appear again,how do i improve it

I use matlab to write this code,and it seems there is something wrong with logic,but i don't know where am i wrong and how to improve this.
i want to write a lotto code,and there are six numbers in it,the range of first six numbers is 1 to 38,the range of last number is 1 to 8.Here is my code
previous_number=randi([1,38],1,6)
last=randi([1,8],1,1) %produce the last number
for k =1:6
while last== previous_number %while that last number is the same as the value of one of the previous number
last=randi([1,8],1,1)%then produce the last number again,until the different value produce
end
end
ltto=[previous_number last]
but i found that the last number will still generate the same number as the first six numbers,for example,
"1" 2 33 55 66 10 "1"
1 "2" 33 55 66 10 "2"
Why?i have already said
while last==previous_number(k)
last=randi([1,8],1,1)
end
if i want to write the code in c or other program language,i think i can just use if ,while and loop,etc,like this basic loop,i can't use the "ismemeber"or randperm. how can i rewrite the code?
if i rewrite as
previous_number=randi([1,38],1,6)
last=randi([1,8],1,1) %produce the last number
for k =1:6
if last== previous_number(k) %while that last number is the same as the value of one of the previous number
last=randi([1,8],1,1)%then produce the last number again,until the different value produce
end
end
ltto=[previous_number last]
the result will also show me "1" 2 21 12 13 22 "1" sometimes
This occures because you first iterate over the numbers, then replace last according to the specific current iteration, without regarding the previous ones.
For example, in your example data, think that last = 10 so you get to the sixth iteration, find that last is equal to b(k) that is 10, so you replace it. But now it can generate 1, and you will finish the while loop and the for loop.
The solution is to compare last to all your vector, not iterate over it:
previous_number = b(1:6);
last = previous_number(1);
while ismember(last, previous_number)
last = randi(8); %produce the last number
end
[As of comments discussion:]
If you still want to compare each element separately, you can do it like that:
previous_number=randi([1,38],1,6)
last=randi(8)
k=0;
while k <= 5
k = k + 1;
if last == previous_number(k)
last = randi(8);
k = 0;
end
end
ltto=[previous_number last]

Can someone explain the TI BASIC 🔺List command?

I understand that the command compares and can subtract values, but I don't see exactly how that works. I've used a TI BASIC programming tutorial site (http://tibasicdev.wikidot.com/movement-explanation) and I need clarification on 🔺List as a whole.
This portion of the code with 🔺List is as follows,:
:min(8,max(1,A+sum(ΔList(Ans={25,34→A
:min(16,max(1,B+sum(ΔList(K={24,26→B
and the website explains the code like this.:
"This is how this code works. When you press a key, its value is stored to K. We check to see if K equals one of the keys we pressed by comparing it to the lists {24,26 and {25,34. This results in a list {0,1}, {1,0}, or {0,0}. We then take the fancy command Δlist( to see whether to move up, down, left or right. What Δlist( does is quite simple. Δlist( subtracts the first element from the second in the previous list, and stores that as a new one element list, {1}, {-1}, or {0}. We then turn the list into a real number by taking the sum of the one byte list. This 1, -1, or 0 is added to A."
The ΔList( command subtracts every element in a list from its previous element. This code uses some trickery with it to compactly return 1 if a key is pressed and -1
ΔList( calculates the differences between consecutive terms of a list, and returns them in a new list.
ΔList({0,1,4,9,16,25,36})
{1 3 5 7 9 11}
That is, ΔList({0,1,4,9,16,25,36}) = {1-0, 4-1, 9-4, 16-9, 25-16, 36-25} = {1 3 5 7 9 11}.
When there are only two elements in a list, ΔList({a,b}) is therefore equal to {b-a}. Then sum(ΔList({a,b})) is equal to b-a, since that's the only term in the list. Let's say that K is 26 in your example; that is, the > key is pressed.
B+sum(ΔList(K={24,26→B Result of expression:
K 26
K={24,26 {0,1}
ΔList(K={24,26 {1} = {0 - 1}
sum(ΔList(K={24,26 -1
B [current x-position of player]
B+sum(ΔList(K={24,26→B [add 1 to current x-pos. of player]
Similarly, B will be decreased if key 24, the left key, is pressed.

Selecting all columns in a cell array that contain a certain value in the first row?

I currently have a 4x3500 cell array. First row is a single number, 2 row is a single string, 3rd and 4th rows are also single numbers.
Ex:
1 1 2 3 3 4 5 5 5 6
hi no ya he ........ % you get the idea
28 34 18 0 3 ......
55 2 4 42 24 .....
I would like to be able to select all columns that have a certain value in the first row. ie if I wanted '1' as the first row value, it would return
1 1
hi no
28 34
55 2
Then I would like to sort based on the 2nd row's string. ie if I wanted to have'hi', it would return:
1
hi
28
55
I have attempted to do:
variable = cellArray{:,find(cellArray{1,:} == 1)}
However I keep getting:
Error using find
Too many input arguments.
or
Error using ==
Too many input arguments.
Any help would be much appreciated! :)
{} indexing will return a comma separated list which will provide multiple outputs. When you pass this to find, it's the same as passing each element of your cell array as a separate input. This is what leads to the error about to many input arguments.
You will want to surround the comma-separated list with [] to create an array or numbers. Also, you don't need find because you can just use logical indexing to grab the columns you want. Additionally, you will want to index using () to grab the relevant rows, again to avoid the comma-separated list.
variable = cellArray(:, [cellArray{1,:}] == 1)

index out of bounds because numel ()

I want to check each of the age from a file named "2". This "2" file contains ages and is formatted in the following way:
2010
1997
0
...
...
The code I have written to do this is below, but it returns an error:
data_register = importdata('DATA/2')
for i = 1:700
year=data_register(i);
age=2014-year(i);
B22(A22<=1)=1;
B22(A22>1&A22<50)=-1;
B22(A22>=50)=1;
D22(i)=B22';
end
feature22= D22'
What am I doing wrong?
The reason why is because year is only a single element. When you move to the next iteration, you are traversing out of bounds as year is just a single element but you are trying to access the non-existent second element.
The reason why year is a single element is due to the year = data_register(i) assignment. You need to change the age assignment to the following:
age = 2014 - year;
Aside
Your for loop doesn't make any sense to me. I'm not sure why you are using age when you aren't using it in the statements that follow it. What is A22? B22? Is it related to age? How are these being calculated?

Arrange data using loop in MATLAB

If I have:
t=(1:1:5)'
time=1:3:100
How do I arrange data t in each column starting from 1 until the end, with an interval of 3. Which means that the data t (1 to 5) at column 1,4,7 and so on.
I've tried:
t=[1:1:5];
nt=length(temp);
time=[1:1:100];
nti=length(time);
x=zeros(nt,nti);
temp=temp';
initiator=2;
monomer=3;
post=1:3:100;
for l=1:post
step=1;
maxstep=100;
while (step<maxstep)
step=step+3;
temp=(1:1:5)';
end
t(:,l)=t;
x=[t];
end
This only shows result X with temp at column 1. I do not know how to to arrange this data at columns that I want.
Hope someone will help me. Thank you in advance.
How many dimensions does your data have? If you already have "temp" (temperature?) and "time" as your first two dimensions and you want "t" to be the third dimension, then create a three-dimension matrix.
To extract from indexes [1 4 7 10 13 16 ... ], use (1:3:end)
To extract from indexed [2 5 8 11 14 17 ... ], use (2:3:end)
In MATLAB's colon notation, the first value is the start. Second value is increment. Third value is the end value and is inclusive.