how to do convolution of two arrays in 3d?

1 view (last 30 days)
Sreeraj R
Sreeraj R on 27 Dec 2021
Commented: Matt J on 28 Dec 2021
hi everyone
i am trying to do convolution between a 371x391x23 and 371x391x23 array. however when i used convn, the output which i got had only Nan values in it. one of the input array has some Nan values in it. is it because of that the output also became Nan? kindly help me to solve it.

Answers (2)

Walter Roberson
Walter Roberson on 27 Dec 2021
Yes, a single nan can pollute more than 95% of the output of a matrix with those dimensions, if it happens to be at the center of the array.
There is pretty much no point is doing a convolution with nan values present
  2 Comments
Walter Roberson
Walter Roberson on 27 Dec 2021
No. If you look at the formula at https://www.mathworks.com/help/matlab/ref/convn.html#bvg3kvh-9 then note that every A value is involved in the computation so if one of them is nan then at least one individual multiplication gives a nan result. But there is the summation over all of the values, and so nan is certain to be involved somewhere in the summation, so you are going to get nan as the result for all locations (except perhaps at some border locations.)

Sign in to comment.


Matt J
Matt J on 27 Dec 2021
Edited: Matt J on 28 Dec 2021
Consider overwriting the NaNs with zeros
A(isnan(A))=0;
or more sophisticated missing data inpainting routines offered on the file exchange, e.g.,
  2 Comments
Walter Roberson
Walter Roberson on 28 Dec 2021
Note that putting in any finite numeric value is going to change the entire meaning of the convolution.
A = magic(8)
A = 8×8
64 2 3 61 60 6 7 57 9 55 54 12 13 51 50 16 17 47 46 20 21 43 42 24 40 26 27 37 36 30 31 33 32 34 35 29 28 38 39 25 41 23 22 44 45 19 18 48 49 15 14 52 53 11 10 56 8 58 59 5 4 62 63 1
B = A.'
B = 8×8
64 9 17 40 32 41 49 8 2 55 47 26 34 23 15 58 3 54 46 27 35 22 14 59 61 12 20 37 29 44 52 5 60 13 21 36 28 45 53 4 6 51 43 30 38 19 11 62 7 50 42 31 39 18 10 63 57 16 24 33 25 48 56 1
C1 = conv2(A, B, 'same')
C1 = 8×8
26200 31667 38214 41674 36264 32252 26200 20313 31667 39261 43609 53452 43219 34581 32837 28310 38214 43609 49537 60238 51552 43804 38019 30123 41674 53452 60238 62224 60238 53452 41674 33160 36264 43219 51552 60238 53632 44194 35874 29603 32252 34581 43804 53452 44194 39261 31082 22070 26200 32837 38019 41674 35874 31082 26785 21873 20313 28310 30123 33160 29603 22070 21873 21764
A(4,4) = nan;
C2 = conv2(A, B, 'same')
C2 = 8×8
NaN NaN NaN NaN NaN NaN NaN 20313 NaN NaN NaN NaN NaN NaN NaN 28310 NaN NaN NaN NaN NaN NaN NaN 30123 NaN NaN NaN NaN NaN NaN NaN 33160 NaN NaN NaN NaN NaN NaN NaN 29603 NaN NaN NaN NaN NaN NaN NaN 22070 NaN NaN NaN NaN NaN NaN NaN 21873 20313 28310 30123 33160 29603 22070 21873 21764
A(4,4) = 0;
C3 = conv2(A, B, 'same')
C3 = 8×8
24165 29928 37252 40416 35413 31697 24054 20313 29669 37559 42610 52157 42405 34063 30654 28310 37770 42869 48168 59165 49924 41880 37834 30123 41193 52675 58906 61188 58573 51491 41526 33160 34377 41628 50442 58832 52929 43787 33580 29603 30402 33027 42657 52009 43528 38891 28751 22070 25608 31949 36798 40749 34098 29010 26748 21873 20313 28310 30123 33160 29603 22070 21873 21764
A(4,4) = 1;
C4 = conv2(A, B, 'same')
C4 = 8×8
24220 29975 37278 40450 35436 31712 24112 20313 29723 37605 42637 52192 42427 34077 30713 28310 37782 42889 48205 59194 49968 41932 37839 30123 41206 52696 58942 61216 58618 51544 41530 33160 34428 41671 50472 58870 52948 43798 33642 29603 30452 33069 42688 52048 43546 38901 28814 22070 25624 31973 36831 40774 34146 29066 26749 21873 20313 28310 30123 33160 29603 22070 21873 21764
sum(C4-C3, 'all')
ans = 1624
sum(A, 'all'), sum(B, 'all')
ans = 2044
ans = 2080
... I can't see any obvious simple relationship
Matt J
Matt J on 28 Dec 2021
Yes, inpaintn() or fillmissing() would probably be better.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!