Find indices for the minimum positive values in a cell

Hi all
i am not able to find the indices for the minimum positive values inside a 2x7 cell. I would find the cell number (containing this minimum element) and its position inside the cell. For example in the first cell the minimum positive value have index [row,column]=[1,2].
This is my 2x7 cell:
C = {[5 0.0001;0 0.1],3,-2,2,3,[-0.1 5],[2 0.0001];[1 2],[0.0001 1],2,4,5,[1 -0.1],6};
Thank you for the help
Regards

 Réponse acceptée

Rik
Rik le 19 Juin 2020
You can always consider cellfun if you want to apply a function to every element of your cell. It might not be the fastest option though.
C = {[5 0.0001;0 0.1],3,-2,2,3,[-0.1 5],[2 0.0001];[1 2],[0.0001 1],2,4,5,[1 -0.1],6};
[rows,cols]=cellfun(@myfun,C);
function [row,col]=myfun(A)
A(A<0)=inf;%make sure it isn't the minimum
[~,idx]=min(A(:));
[row,col]=ind2sub(size(A),idx);
end

9 commentaires

Thank you for the reply
the code you have written include zero as minimum positive element, i don't want it. I have tried to modify the function in this way:
function [row,col]=myfun(A)
A(A<0)=inf;%make sure it isn't the minimum
[~,idx]=min(A(A>0));
[row,col]=ind2sub(size(A),idx);
end
but with no success, it always include zero.
You should have modified the first line.
A(A<=0)=inf;%make sure it isn't the minimum
% ^ that will change 0 to inf as well
thank you very much
Rik
Rik le 19 Juin 2020
You're welcome. If you feel I solved your issue, please consider marking my answer as accepted. If not, feel free to comment with your remaining issues.
EldaEbrithil
EldaEbrithil le 19 Juin 2020
Modifié(e) : EldaEbrithil le 19 Juin 2020
Sorry for the disturb but i want to specialise more my code. Using this new cell array i have found something interesting:
A = {[-5 0.0001;0 0.1],[3 -1],[-2 4],[2 -1],[3 -4],[-0.1 5],[2 -0.0001];[1 -2],[0.0001 -1],[2 -1],[4 -7],[5 -6],[-1 -0.1],[6 -7]};
i put deliberately one cell with only negative value. The code previously written is able to evaluate the minimum positive element of each cell but for the negative cell give a negative value that is unacceptable for my purposes. So i have written the code below:
function [row,col,Row,Col,M,m]=myfun(A)
if abs(max(A(A<0)))<min(A(A>=0))
[M,idx]=max(A(A<0));
[row,col]=ind2sub(size(A),idx);
if abs(max(A(A<0)))>min(A(A>=0))
[m,Idx]=min(A(A>0));
[Row,Col]=ind2sub(size(A),Idx);
end
end
end
this code theoretically evaluate the indeces for the cell value closer to zero (whether it is positive or negative). The < and > signs create some problems with cells. How can i bypass this problem?
Rik
Rik le 19 Juin 2020
For [-1 -0.1], what output do you want? Should the index being returned be NaN? Empty?
Why did you add the extra outputs to that function? What did you try to achieve with these edits?
1)-0.1
2)Well in pratical there will be always a possible value because i am searching for the closest to zero value in each cell (whether it is positive or negative it makes no difference, the important thing is that it must be as close as possible to zero).
3)It migth be the wrong approach
4)I want to find, for each cell, which are the indeces of thel closest to zero elements
If you mean closest to 0, why did you say only positive? And do you want the actual value itself, or the indices, as you originally described?
Since inside the function you're working with a local copy of the array, you can simply use the abs function to remove the sign.
function [row,col]=myfun(A)
A=abs(A);
[~,idx]=min(A(:));
[row,col]=ind2sub(size(A),idx);
end
Please stop changing the requirements. Note that you can run this function as normal:
[row,col]=myfun([-1 -0.1]);
Think long and hard about what it is you want. Only then can you hope to solve this.
EldaEbrithil
EldaEbrithil le 19 Juin 2020
Modifié(e) : EldaEbrithil le 19 Juin 2020
Sorry I had not specified very well in the last message. When i introduced the negative cell I wanted to take a step forward, redirecting myself to another goal, that is to find the value closest to zero. Thank you very much for the help

Connectez-vous pour commenter.

Plus de réponses (1)

Suhas Maddi
Suhas Maddi le 19 Juin 2020
Modifié(e) : Suhas Maddi le 19 Juin 2020
Hii Elda,You can use a for loops to go through each element in the cell and find indexes of minimum values for each cell element using the min() function.For more information and detailed examples, You can refer to : https://www.mathworks.com/help/matlab/ref/min.html
%You want to find minimum element and it's index in first Cell element
%M is the minimum value and [x,y] is the index of the minimum value in the cell element.
[M,I]=min(min(C{1,1}));
[x,y]=find(C{1,1}==M)
%To do this for the whole cell,You can use for loops to iterate through each element.
I hope this helps you.

3 commentaires

Thank you for the reply
do you mean something like that?
for i=1:2
for j=1:7
%You want to find minimum element and it's index in first Cell element
%M is the minimum value and [x,y] is the index of the minimum value in the cell element.
[M,I]=min(min(C{i,j}));
[x,y]=find(C{i,j}==M);
%To do this for the whole cell,You can use for loops to iterate through each element.
end
end
Hii Elda,Yes Kind of in the lines of what you have mentioned.If you want to store the indices of minimum elements for each cell element,You can use a vector or matrix or another cell array Hope this helps.
ok something like that i suppose:
for i=1:2
for j=1:7
%You want to find minimum element and it's index in first Cell element
%M is the minimum value and [x,y] is the index of the minimum value in the cell element.
[M(i,j),I]=min(min(C{i,j}));
[x(i,j),y(i,j)]=find(C{i,j}==M(i,j));
%To do this for the whole cell,You can use for loops to iterate through each element.
end
end
But i want only >0 terms and this code include 0 as minimum element

Connectez-vous pour commenter.

Catégories

Produits

Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by