Extract values from matrix using symbolic function
이전 댓글 표시
I am coding a live function, of which a matrix B is the input. I am trying to define a symbolic function b(a,c) which extracts the (a,c)th entry of the matrix B, B(a,c). As such my code is as follows (please note that this is the very first part of my live function)
syms a c
assume(a, "integer")
assume(c, "integer")
syms b(a,c)
b(a,c) = B(a,c);
However, when I try to run it, I get the error "Error using indexing Invalid indexing or function definition. Indexing must follow MATLAB indexing. Function arguments must be symbolic variables, and function body must be sym expression."
Why does this happen and how do I fix it?
댓글 수: 3
Dyuman Joshi
2024년 3월 4일
To use a and c (or any other variables) as indices, you need to define them as numeric (or logical) values (which is what the MATLAB indexing stated in the error message is).
Symbolic variables are not accepted as indices.
Chi Chung Shum
2024년 3월 4일
You can want to do something. But that does not mean it is possible. Language syntax is what it is, and you cannot arbitrarily define what you want and assume it will work. A symbolic variable cannot be used as an index.
But there seems to be no reason to have defined a and c as symbolic. Just because you don't currently know the value of a variable, does not mean it MUST be symbolic. For example, you can define a function handle.
extrct = @(B,a,c) B(a,c);
B = magic(5)
extrct(B,2,3)
Honestly though, I'm not sure I see that as any improvement over the simpler direct indexing:
B(2,3)
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Number Theory에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!