apply a function to every element of a square matrix

조회 수: 14 (최근 30일)
Salvatore Mazzarino
Salvatore Mazzarino 2012년 9월 24일
I have a square matrix and I would apply a function to every element of that matrix in the way I get a new square matrix where every element is the result of the function.does it exist a function for the purpose?

채택된 답변

Wayne King
Wayne King 2012년 9월 24일
편집: Wayne King 2012년 9월 24일
Yes, arrayfun()
For example:
x = randn(10,10)+1i*randn(10,10);
y = arrayfun(@conj,x);
or
x = randn(10,10);
y = arrayfun(@(x) x.^2,x);
  댓글 수: 1
Wayne King
Wayne King 2012년 9월 24일
Simon's point is a good one here. Both the examples I gave are easily handled just as:
y = conj(x);
% or for the second
y = x.^2;
I was just illustrating the general use of arrayfun().

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Jan
Jan 2012년 9월 24일
Usually arrayfun is not required and wastes time:
x = rand(10, 10);
a = sin(x);
b = x .* x;
c = x .^ 2;

Image Analyst
Image Analyst 2012년 9월 24일
Don't forget other cases, non-point processing cases, where vectorized solutions don't work, and I don't think arrayfun would work easily either. For example area processing operations like Sobel Filter, Savitkzy-Golay filter, morphological operations, adaptive filters, etc. Some of these have special functions to do them, but in general, for your custom operations, you can use functions like conv2(), imfilter(), nlfilter(), blockproc(), etc.

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by