Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
find the max number between two indice of a vector

Subject: find the max number between two indice of a vector

From: Hajik zagori

Date: 12 Sep, 2011 17:22:10

Message: 1 of 2

Hi, I want a code to find the maximum number between two indice of a vector.
I have an indice vector and a data vector . For example my indice vector is :
A=[3 7 13]
and my data is a vector like this :
B=[-5 3 -8 1 -7 2 -9 3 6 2 7 9 -4 2 6]

now the max number between indices 3 and 7 of vector B is number "2" .
and the max number between indices 7 and 13 of vector B is number "9".

So the output should be like a vector named C as follows:

C=[2 9]

I've been thinking on this for hours but so far haven't found any solution :(

Tnx in advance.

Subject: find the max number between two indice of a vector

From: Miroslav Balda

Date: 14 Sep, 2011 17:14:28

Message: 2 of 2

H Hajik,
It is not so difficult to find the required solution:

% Hajik.m 2011-09-14
%%%%%%%%%%%%%%%%%
A = [3 7 13];
B = [-5 3 -8 1 -7 2 -9 3 6 2 7 9 -4 2 6];
n = length(A)-1;
C = zeros(1,n);
for k = 1:n
    C(k) = max(B([A(k):A(k+1)]));
end
disp(C)

Hope it helps.
Mira

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us