필터 지우기
필터 지우기

cellfun with Frobenius norm

조회 수: 3 (최근 30일)
Xh Du
Xh Du 2017년 3월 3일
댓글: Xh Du 2017년 3월 3일
Hi all,
I'd like to obtain Frobenius norm for each element in a cell, I wrote this:
a1 = (1:6)';
a2 = (2:7)';
a3 = (3:8)';
a = {a1 a2 a3};
nm.a = cellfun(@norm, a);
However, if I want a Frobenius norm, I'll need norm(x, 'fro'), how can I add 'fro' in this case in cellfun?
Thank you!

채택된 답변

Guillaume
Guillaume 2017년 3월 3일
Use an anonymous function:
nm.a = cellfun(@(m) norm(m, 'fro'), a)
  댓글 수: 3
Guillaume
Guillaume 2017년 3월 3일
Looks compact enough to me. If it bothers you that much, create an m file called frobeniusnorm.m with the content:
function m = frobeniusnorm(x)
m = norm(x, 'fro');
end
You can then write your cellfun more compactly:
nm.a = cellfun(@frobeniusnorm, a);
Xh Du
Xh Du 2017년 3월 3일
Many thanks, I was just wondering is there a possibility to write without an anonymous function.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by