他の答えで述べたように、関数に配列を入力するだけの場合もあります。しかし、あなたの機能が実行する操作に応じて、間違った結果が得られる可能性があります。あなたはそれを避けたい場合は、単にこのように、あなた自身のarrayfun
を定義することができます。
function B = matrixfun(func,A)
for i = 1 : size(A,'r')
for j = 1 : size(A,'c')
B(i,j) = func(A(i,j))
end
end
endfunction
それがより一般的であるので、私はmatrixfun
それを呼びました。一例として、を実数を16進数として表す文字列に変換する関数toHex
を考えてみてください。
--> disp(A) //original random matrix
37. 14. 4. 42.
17. 2. 18. 48.
23. 37. 31. 21.
16. 39. 43. 17.
--> disp(B) //wrong results
!5 14 4 10 !
! !
!1 2 2 0 !
! !
!7 5 15 5 !
! !
!0 7 11 1 !
--> disp(C) //right results
!25 E 4 2A !
! !
!11 2 12 30 !
! !
!17 25 1F 15 !
! !
!10 27 2B 11 !
:
function s = toHex(n)
s = "";
n = round(n)
if n == 0 then
s = "0";
else
while n >= 1
select modulo(n,16)
case 15 then s = "F" + s
case 14 then s = "E" + s
case 13 then s = "D" + s
case 12 then s = "C" + s
case 11 then s = "B" + s
case 10 then s = "A" + s
else s = string(modulo(n,16)) + s;
end
n = floor(n/16)
end
end
endfunction
A = grand(4,4,"uin",0,50); //random matrix
B = toHex(A);
C = matrixfun(toHex,A);
コンソールで結果を確認することができます