[英]finding indices of min or max calculated in a range of an array or matrix in matlab
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. 我想计算范围矩阵中所有A列中的最小值及其索引。 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? 如何不使用循环方法就得到
range(i:i+1)
的min(A)
索引?
Simply put, matrix 'B' should be like 简而言之,矩阵“ B”应类似于
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'? 我的问题是如何在没有“ for循环”的情况下进行操作?
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.