Finding maximum index of a row of array in MATLAB - arrays

I want to get the index of a specific row in a 2-D array. I have a 15*15 array (Q), and I want to get index of the maximum number of 2nd row.
I wrote this code, but an error occurred:
y=Q(2,:);
x=max(y)
??? Subscript indices must either be real positive integers or logicals.

You are getting the maximum value of the second row, but you want the index of the maximum value. Here's how to get the index:
[~, index] = max(y)

Related

How to get number of column in an Array array?

If I have a two-dimensional array, how can I get the number of columns ?
I can get number of lines with Array.length(o) where o is an 5*10 matrix, which returns 5.
But how to do with columns?
Assuming there is at least one "line", and that the column arrays are of equal length, just take the length of the first "column" array:
Array.length m.(0)
If there are no lines however, this will raise an exception.

How to find the index value of the quartile values in an array using Matlab?

I have a vector of dimension 1x3000. I have found the percentile value using the percentile function in Matlab. But I am unable to find the index value of the quartile inside the vector.
y = rand(1,3000);
Q_2 = prctile(y,50);
Idx = find(y==Q_2);
Idx is returning an empty value. I should be able to get a value of the index containing the median value.
You can efficiently find the entry closest to the median (or an arbitrary q_2 for that matter) with:
[~,Idx]=min(abs(q_2-y));
As per help min, the value returned as Idx corresponds to the first element with the minimum value in the vector of differences.

How to divide one array with unknown number of values into sub-arrays in matlab

I have an array in MATLAB which the total number of elements is unknown, but it is always multiple of 32. How can I transform this array into a cell array in which each of its positions will contain 32 values?
You can use num2cell:
a = rand(1,32*5);
result = num2cell(reshape(a,[],32),2);

Number of occurrences of a number in a particular range?

Given a large unsorted array, I need to find out the number of occurrences of a given number in a particular range. (There can be many queries)
e.g. if arr[]={ 6,7,8,3,4,1,2,4,6,7,8,9} and left_range=3 and right_range=7 and number=4, then the output will be 2. (considering a 0 indexed array)
arr[i] can be in the range of 1 to 100000. The array can have up to 100000 numbers.
Can you guide me about which data structure or algorithm I should use here?
PS: Pre-processing the array is allowed.
Here's a solution that doesn't require segment tree.
Preprocessing:
For each number arr[i], push i to the 2D vector(or ArrayList) with index arr[i].
Answering Queries:
For any query do a binary search on vector[num] to find the index of the maximum index of num in that vector that's less than or equal to right range, let's call it R. Then find the minimum index that's greater than or equal to left range, let's call it L. Print R - L + 1
Runtime:
Preprocessing in O(1) per item, taking total O(N) time.
Per Query answer: O(lg(N))
Space: Quite linear assuming vector or ArrayList

Arrays: find two numbers such that their sum is maximum with some additional conditions

Given a set of numbers and a number k, find the maximum sum such that if you pick a number at index i you should not pick any number from index i - K to index i + K.
This problem was asked in google to my friend. I am not able to figure out a solution better then a naive O(n^2).
You can do this in O(n) by keeping track of the maximum of all values seen in the first i-K-1 entries in the array.
Python code:
A=[3,9,10,3,6,7,1,5]
K=2
m=A[0]
bestsum=0
for i in xrange(K+1,len(A)):
m=max(A[i-K-1],m) # stores maximum of values in A[0],A[1],...,A[i-K-1]
bestsum=max(bestsum,A[i]+m)
print bestsum
For each index i we combine A[i] with m which is the highest value seen in the initial values of the array A[0],..,A[i-K-1].
You may be able to modify this http://www.geekviewpoint.com/java/dynamic_programming/positive_subset_sum or http://www.geekviewpoint.com/java/dynamic_programming/max_subarray_sum.
Sorting the array before may not be acceptable for the question

Resources