필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How to "vectorize" this operation?

조회 수: 1 (최근 30일)
Peter Laslo
Peter Laslo 2013년 4월 12일
마감: MATLAB Answer Bot 2021년 8월 20일
Hi,
I have data of sample size m of n by n matrices in an n by m*n matrix call it P.
I also have a function, call if f, that operates on a fixed vector, call it v, and n by n matrices.
I want to to create a 1 by m vector, call it d, by operating f on v and each of the n by n matrices in P.
So, say for example, n = 3 and m = 6 I would want:
d(1) = f(v,P(:,1:3)), d(2) = f(v,P(:,4:6)), . . ., d(6) = f(v,P(:,16:18))
How can I do this without making a loop?
Thanks!
  댓글 수: 3
Peter Laslo
Peter Laslo 2013년 4월 12일
f is a real-valued function of the the vector v and the matrices. So I'm trying to create 1 by m vector of real-numbers.
Matt J
Matt J 2013년 4월 13일
편집: Matt J 2013년 4월 13일
Whether that's possible without a loop would depend on the specific form of f(), i.e., the operations that f consists of.

답변 (1개)

Matt J
Matt J 2013년 4월 12일
This looks like it avoids a for-loop, but doesn't really.
P=reshape(P,n,n,m);
d= arrayfun(@(i) f(v,P(:,:,i)) , 1:m);
  댓글 수: 1
Peter Laslo
Peter Laslo 2013년 4월 12일
편집: Peter Laslo 2013년 4월 12일
Thanks, I thought this would work:
a = 1:m;
d = f(v,P(:,:,a))
but the function tries to operate on P all at once (instead of the individual matrices) and I get an error.

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by