Reputation: 3
If I have a matrix
A=rand(50,4);
and a matrix of range values like
range=[1 10 15 30 45 50];
and i want to calculate minimum value and its index in all A columns over the range matrix. for example
for i=1 :numel(range)-1
[value index]= find(min(A(range(i):range(i+1),[],1)
if i>1
index=index+ range(i)+1; % indx# was calculated onlyin a
% range of array and not for
%whole array.Updated here
end
B_ind(i,:)=index;
B_val(i,:)=value;
end
How can I get indices of min(A)
for range(i:i+1)
without using a loop approach?
Simply put, matrix 'B' should be like
B(1,column 1:4)=index of min(A (1:10)) in every column
B(2,column 1:4)=index of min(A (11:15)) in every column
B(3,column 1:4)=index of min(A (16:30)) in every column
and so on...
My question is how to do it without a 'for loop'?
Upvotes: 0
Views: 165
Reputation: 899
One possibility would be:
% generate example data set
A=rand(50,4);
range=[1 10 15 30 45 50];
% generate indexes of interest
tmp_idx= arrayfun(@colon,range(1:end-1),range(2:end),'un',0);
% calculate the min function over the indexes
[min_value,min_idx]=cellfun(@(x) min(A(x,:)), tmp_idx, 'UniformOutput', false);
% get matrix B (with the offset of ranges)
B=cell2mat(min_idx')+(range(1:end-1)+1)';
B(1,:)=B(1,:)-2;
Upvotes: 2