Matlab Code: Eval and Sub2ind
Show older comments
Hi, I come through this code and i couldn't understand how it works.
It is combination of eval and sub2ind. Could anybody explain to me with this problem. How to get the final answer = 381?
Thanks in advance.
Problem
ngrid=20
ndim=2
idnames =',1,20'
ANS = eval(['sub2ind(ngrid.*ones(1,ndim)' idnames ');'])
Accepted Answer
More Answers (2)
Alternate code avoiding eval():
ngrid=20;
ndim=2;
idnames = {1,20};
ANS = sub2ind(ngrid.*ones(1,ndim), idnames{:})
Image Analyst
on 10 Apr 2023
0 votes

Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!