필터 지우기
필터 지우기

functions instead eval?

조회 수: 1 (최근 30일)
eng had
eng had 2020년 5월 26일
답변: rubindan 2020년 5월 27일
Hi everyone
How can I evalute a randomly generated equation as String,without using eval, to speed up my code
like:
'0.81472/mask(4)+mask(16)*mask(7)/mask(24)/mask(4)-mask(24)+mask(21)/mask(11)/mask(20)+mask(17)/mask(22)/mask(17)-mask(19)+mask(17)+mask(18)+mask(7)/mask(3)-mask(18)+mask(24)-mask(11)/mask(20)-mask(5)*mask(12)/mask(18)*mask(7)+mask(17)-mask(3)'
ps mask is an image mask 5*5 with changable values.
  댓글 수: 18
per isakson
per isakson 2020년 5월 27일
This might be a XY Problem. Instead of trying to evaluate an expression, which is representeted by a string, it might be better to search for a more useful way to represent the expression.
Image Analyst
Image Analyst 2020년 5월 27일
편집: Image Analyst 2020년 5월 27일
You assigned the tag "image processing". Why? I don't see any image processing in what you've presented so far. I don't see how assigning random operations will help you "detect the relation between the pixles in the sliding window". What relation do you hope to discover?
But I think what people would really like to hear is the use case. Why do you need to do this? What is the background? Please give us some context as to why you need to do this. Is it homework, or some real world research into something (if so, what)? Knowing that, people may be able to suggest a better approach.

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

답변 (1개)

rubindan
rubindan 2020년 5월 27일
You can use str2func as follows:
rnstr = ['0.81472/mask(4)+mask(16)*mask(7)/mask(24)/mask(4)',...
'-mask(24)+mask(21)/mask(11)/mask(20)+mask(17)/mask(22)/mask(17)',...
'-mask(19)+mask(17)+mask(18)+mask(7)/mask(3)-mask(18)+mask(24)',...
'-mask(11)/mask(20)-mask(5)*mask(12)/mask(18)*mask(7)+mask(17)-mask(3)'];
str = ['@(mask) ',rnstr]; % add "mask" as the input
fh = str2func(str) % a function handle
val = fh(rand(1,50)); % value for some rnadom input

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by