而不是使用find
,請使用min
函數中的ind
output。這是的linear index。要做到這一點,你可以使用ind2sub
:
[r,c] = ind2sub(size(resultant),ind);
這不是很清楚你是什麼意思resultant = A+B+C
因爲你清楚,如果你得到一個更大的陣列(72x64800)之和不等於他們,而另一方面,這不是簡單的連接([A B C]
),因爲這會導致72x811陣列。
然而,假設這是串聯,你可以做到以下幾點:
% get the 2nd dimension size of all matrices:
cols = cellfun(@(x) size(x,2),{A,B,C})
% create a vector with reapiting matrices names for all their columns:
mats = repelem(['A' 'B' 'C'],cols);
% get the relevant matrix for the c column:
mats(c)
所以mats(c)
將與最小值的矩陣。
編輯:
從您的評論我的理解是你的代碼看起來是這樣的:
% arbitrary data:
A = rand(72,1);
B = rand(72,720);
C = rand(72,90);
% initializing:
K = size(B,2);
N = size(C,2);
counter = 1;
resultant = zeros(72,K*N);
% summing:
for k = 1:K
for n = 1:N
resultant(:,counter) = A + B(:,k) + C(:,n);
counter = counter+1;
end
end
% finding the minimum value:
[minimumValue,ind] = min(resultant(:))
,並從你知道你可以做到這一點的回答開始:
[r,c] = ind2sub(size(resultant),ind)
獲取行和列minimumValue
於resultant
。所以,以同樣的方式,你可以這樣做:
[Ccol,Bcol] = ind2sub([N,K],c)
其中Bcol
和Ccol
是分別B
和C
,列,因此:
minimumValue == A(r) + B(r,Bcol) + C(r,Ccol)
要看看它是如何工作的設想,循環上面填充矩陣M,其值爲counter
,並且M的大小爲N
-by- K
。因爲我們用線性索引填充M,它將以列主要方式填充,因此該行將對應於迭代器n
,該列將對應於迭代器k
。現在c
對應於我們得到了最小值counter
,並在中號的行和列counter
告訴我們在B
和C
列,所以我們可以再次使用ind2sub
獲得的counter
位置的下標。當然,我們並不需要創建M,因爲它內部的值只是線性索引本身。
大概64800有事情做與720 * 90 * 1 = 64800 ...但我同意,它仍然遠不清晰的問題是什麼 –
結果實際上是這種形式:'結果= A + B(:, ⅰ)+ C(:,J);其中我= 720和j = 90' –
@summyiaharis請參閱我的編輯的答案 – EBH