Clear Filters
Clear Filters

How can I find the index of the maximum Value in a given subintervall in the normal array

2 views (last 30 days)
Hi community,
first time asking a question here. I am currently dealing with task involving this problem:
For simplification, lets say, I have two corresponding arrays:
Current_array = [0 1 2 3 4 5];
Voltage_array = [1 2 3 4 5 6];
I want to find the index of the maximum Value in the current_array, but only in the regime, where the corresponding Voltage_array is smaller than 2:
[~,index] = max ( (Current_array (Voltage_array > 2) ) );
My output index would be 4, since the largest value of my subarray (Current_array (Voltage_array > 2) ) = [3 4 5 6] is at position 4.
But what I really want is to find the index of that maximum value in my Current_array, which would be index = 6.
Is there an elegent way to solve this?

Accepted Answer

David Hill
David Hill on 7 Apr 2020
[~,index]=max(Current_array.*(Voltage_array>2));
  1 Comment
Kaihua Zhang
Kaihua Zhang on 17 May 2020
Hi David,
thanks for answering my question. After using this method for a while, I stumbled across another problem, for which I have not found a solution yet.
Let's say my current array is not sorted like the current array in the initial post, and I want to retrieve the index of the largest value from the Current_array for a Voltage > 1.
Current_array = [1,3,5,2,4];
Voltage_array = [0,1,2,3,4];
[~,index]=max(Current_array.*(Voltage_array>1));
By using your solution, my index output would be index = 5, since the product between both arrays reaches an maximum at the 5th index. But the desired output should be index = 3.
Do you have another workaround for this?
Thanks in advance,
Kai

Sign in to comment.

More Answers (0)

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!