Error with using vector(end) to get last index number

I have the following line of code:
if temp_small_time(end) ~= temp_large_time(end)
It's producing an error that says Subscript indices must either be real positive integers or logicals. I just want to know if the value at the end of each vector is the same, but I don't "know" where the end is, thus, I'm using the end keyword. What is wrong here?

3 commentaires

You can know where the end is by using "length(X)" (if 1 line/column or "size(X)" if it is an MxN matrix ..
I also get the same error when I use length()
if temp_small_time(length(temp_small_time)) ~= temp_large_time(length(temp_large_time))
What does your matrixes look like? Could you add the code?

Connectez-vous pour commenter.

Réponses (3)

Star Strider
Star Strider le 29 Avr 2016

0 votes

Sometimes an error will occur pointing to a line that is not actually where the error occurred. Check the line just before it to see if there’s some sort of problem with that line.

2 commentaires

The line before it is the keyword end for an if statement.
Since it’s an if block, it would be best to post the entire if block, since the problem would likely occur only if the particular statement was executed.
Seeing the code could give a clue.

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 29 Avr 2016

0 votes

It sounds like you assigned a value to a variable named "end"

3 commentaires

The only other place I use "end" is to finish my if statements and for loops.
I did that experiment (assigning a variable to ‘end’). MATLAB has serious problems with that, and throws an assignment error.
Your array is empty. Accessing an empty array at (end) results in that error message.

Connectez-vous pour commenter.

Jadee Kellogg
Jadee Kellogg le 29 Avr 2016

0 votes

I figured it out, thanks everyone! I actually emptied by vector earlier in the code on accident so there was no end because there was nothing in it.

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by