MATLAB Help - Comparing values/Indexing

  • Thread starter nvalia
  • Start date
  • Tags
    Matlab
In summary, the conversation involves a beginner in MATLAB struggling to find the first relative maximum in a graph using indexing and for loops. The problem is solved by correcting the loop to use integers instead of a decimal increment.
  • #1
nvalia
4
0

Homework Statement



I am very, very new to MATLAB, and can't seem to figure out this seemingly simple problem.

Given a t-array and a y-array for a decaying exponential function, my task is to find the first relative maximum in the graph without using Matlab's preset relative max. functions. I'm attempting to do this by comparing y-values at a certain "t" to y-values at "t0" and "t1" (before and after "t"). I'm using indexing, for loops, etc.

Homework Equations



N/A

The Attempt at a Solution



function[max,t_max] = relMax(t,y)
for i = 1:.001:length(t);
y(:,i+1) = y;
y0 = y(:,i);
y1 = y(:,i+2);
if (y0<y && y>y1)
max = y;
disp(max)
t_max = t;
disp(t)
break
end
end

---

Clearly I am making some kind of stupid mistake here. I don't really know how to use indexing very well, and it's causing me problems.

Thanks in advance!
 
Last edited:
Physics news on Phys.org
  • #2
nvalia said:

Homework Statement



I am very, very new to MATLAB, and can't seem to figure out this seemingly simple problem.

Given a t-array and a y-array for a decaying exponential function, my task is to find the first relative maximum in the graph without using Matlab's preset relative max. functions. I'm attempting to do this by comparing y-values at a certain "t" to y-values at "t0" and "t1" (before and after "t"). I'm using indexing, for loops, etc.


Homework Equations



N/A


The Attempt at a Solution



function[max,t_max] = relMax(t,y)
for i = 1:.001:length(t);
y(:,i+1) = y;
y0 = y(:,i);
y1 = y(:,i+2);
if (y0<y && y>y1)
max = y;
disp(max)
t_max = t;
disp(t)
break
end
end

---

Clearly I am making some kind of stupid mistake here. I don't really know how to use indexing very well, and it's causing me problems.

Thanks in advance!

Indexes must be integers. You should make your loop as
for i = 1: length(t)

0.001 is the increment in t, not in the index.
 
  • #3
Thank you! I figured out what I was doing wrong. :)
 

What is the purpose of comparing values in MATLAB?

Comparing values in MATLAB helps to identify relationships and patterns between different sets of data, allowing for better analysis and decision making.

How do I compare values in MATLAB?

To compare values in MATLAB, you can use logical operators such as "<", ">", "=", and "!=" to evaluate conditions and return a true or false result. You can also use built-in functions like "ismember" and "isequal" to compare arrays and cells.

What is indexing in MATLAB?

Indexing in MATLAB is the process of accessing specific elements or sub-arrays within a larger array or data structure. It allows for efficient manipulation and analysis of data without having to modify the entire set.

How do I index in MATLAB?

To index in MATLAB, you can use square brackets after the variable name to specify the elements or sub-arrays you want to access. You can also use logical indexing to select elements based on certain conditions.

Can I compare and index at the same time in MATLAB?

Yes, you can compare and index at the same time in MATLAB by using logical indexing. This allows you to select specific elements that meet certain conditions and then perform operations on them.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
938
  • Engineering and Comp Sci Homework Help
Replies
2
Views
825
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
995
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
954
Back
Top