Applying a function to each row vector of a matrix
32 ビュー (過去 30 日間)
古いコメントを表示
I have a function that I want to apply to each row of a 7800x784 matrix. The function returns a scalar and puts it into a 7800x1 matrix. So, each row of the first matrix is computed one by one and the output is sent to the second matrix. s and Ns are both 1x26 matrix. Following is my function:
function [ind] = naive_bayes(X, s, Ns)
prob = zeros(1,26);
x = 1;
y = 1800;
for i = 1:size(s,2)
like = sum(X(x:y,:))/s(1,i);
prob(1,i) = Ns(1,i) * prod((like.^X).* (1-like).^(1-X));
x = y + 1;
y = y +1800;
end
[~,ind] = max(prob);
end
Thank you!
0 件のコメント
回答 (1 件)
Walter Roberson
2018 年 4 月 7 日
編集済み: Walter Roberson
2018 年 4 月 7 日
result = arrayfun(@(ROWIDX) naive_bayes(YourArray(ROWIDX,:)), (1:size(YourArray,1)).');
However, your function has the difficulty that it uses x and y and s without those having been defined.
3 件のコメント
Walter Roberson
2018 年 4 月 7 日
result = arrayfun(@(ROWIDX) naive_bayes(YourArray(ROWIDX,:),s,Ns), (1:size(YourArray,1)).');
参考
カテゴリ
Help Center および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!