Advanced Matrices addition help

Im trying to figure out how I can create a function that will take a row vector, say 3-x-n, and figure out how to add each element of its immediate neighbor,find the average and replace create a new matrix.
so if A=[ 1 2 0 1]
[]=avg#(A)
ans= 1.5 1 1 .5
can you help

回答 (2 件)

John D'Errico
John D'Errico 2014 年 4 月 10 日

0 投票

(I'm not sure how a 3xn matrix is a row vector, by the way.)
Anyway, what have you tried? Surely this is trivial with a loop. If you really want to be efficient, then why not try conv? That is usually the trick to all of these problems. Take a shot at it.

3 件のコメント

Joseph Pauwels
Joseph Pauwels 2014 年 4 月 10 日
es, sorry, should have reread my question a 3xn is not a row vector, I meant to say I want the function file to work for a row vector as well as a 3xn. Ill try conv.
Thank you, I dont think thats what I am looking for, I now I need a loop but Im not sure how to start. If i have a 2 x 2 like
a=[2 4 2 3]
the function needs to take (r1+(r1,c1)+(r2,c1)+(r2,c2)) the average them.
Jan
Jan 2014 年 4 月 10 日
I do not understand, what "(r1+(r1,c1)+(r2,c1)+(r2,c2))" means.
Image Analyst
Image Analyst 2014 年 4 月 11 日
Joseph's "Answers" moved here since they're not answers to his original question, butt comments to John's answer:
yes, sorry, should have reread my question a 3xn is not a row vector, I meant to say I want the function file to work for a row vector as well as a 3xn. Ill try conv.
Thank you,
I dont think thats what I am looking for, I now I need a loop but Im not sure how to start. If i have a 2 x 2 like
a=[2 4 2 3]
the function needs to take (r1+(r1,c1)+(r2,c1)+(r2,c2)) the average them.

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

Image Analyst
Image Analyst 2014 年 4 月 11 日

0 投票

I gave your answer in the duplicate question:
sum_Of_a = conv2(a, ones(3), 'same')
count_in_window = conv2(ones(size(a)), ones(3), 'same')
theMean = sum_Of_a ./ count_in_window % Same as "output"

1 件のコメント

Joseph Pauwels
Joseph Pauwels 2014 年 4 月 15 日
can you please delete this question and anwser thread.

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

カテゴリ

タグ

質問済み:

2014 年 4 月 10 日

コメント済み:

2014 年 4 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by