Non zero min 3D matrix

7 ビュー (過去 30 日間)
VISHNUPRIYA M S
VISHNUPRIYA M S 2019 年 3 月 12 日
編集済み: madhan ravi 2019 年 3 月 12 日
I have 3D matrix of A(k,l,t) where t is time. I want to find the non zero min of each row for every t.
for t =1:10
for k = 1:10
f(k,1,t) = max(A(k,:,t)(A(k,:,t)>0));
end
end
it shows error ()-indexing must appear last in an index expression.
I was trying to use f = min(A(A>0))

採用された回答

KSSV
KSSV 2019 年 3 月 12 日
A = rand(10,10,10) ;
for t =1:10
for k = 1:10
D = A(k,:,t) ;
D = D(D>0) ;
f(k,1,t) = max(D);
end
end

その他の回答 (1 件)

Raghunandan V
Raghunandan V 2019 年 3 月 12 日
You can remove all the for loops and make it more effecient
A= rand(10,10,10);
A(A==0) = inf;
min(A)
Here I am just replacing the 0 with inf and then finding the minimum. This code even works for matrix with negative integers. :)
  5 件のコメント
Raghunandan V
Raghunandan V 2019 年 3 月 12 日
Please check the question. the formula written there is max(A(k,:,t). this actually means they are trying to find the minimum of a column as 2nd argument is :. If you need the minimum or rows you can transpose the matrix and then use minimum.
:)
madhan ravi
madhan ravi 2019 年 3 月 12 日
編集済み: madhan ravi 2019 年 3 月 12 日
+1, Ah didn’t examine the code because OP mentioned row in the statement but in the code turns out to find for columns, good proposal then ;-) .In your code what will happen if a column is all zeros? Why do you say if that the second solution proposed by me will not work?, it was the continuation of your code after replacing zeros with infs. By the way when you said vice versa matrix has to be transposed that’s what permute() does , by the way min() function can be used to find the minimum along the specific dimension.If the dimension of A is dynamic then
permute(A,[2 1 3:ndims(A)])

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by