Matrix Dimension must agree - Vectorization
显示 更早的评论
Hi there,
I created a function called Fx which takes an single number as input and produce a 1 single number as output.
I tried to run:
n = 1:10;
r = Fx(n);
What I expected is an array of r 1x10, each contains the result of Fx of n from 1 to 10.
However, I got "Matrix dimensions must agree" instead. This does not make sense to me. I tried to run it with for loop and it worked just fine.
for n = 1:10
r(n) = Fxl(n);
end
The array involved in the calculation of Fx is 115824. So I tried with array n of 1x115824 then the code only return the result of n(1,1) only.
Does anyone know what's going on here?
If so, please let me know.
Thank you
9 个评论
KSSV
2020-7-13
What is ComplexIntegral?
madhan ravi
2020-7-13
Without seeing the function , how can a solution be suggested?
Toan Nguyen
2020-7-13
编辑:Toan Nguyen
2020-7-13
madhan ravi
2020-7-13
Did you use w inside the function?
Toan Nguyen
2020-7-13
madhan ravi
2020-7-13
r = cell(10, 1); % before loop
r{n} = ... % inside loop the rest remains unchanged
Toan Nguyen
2020-7-13
madhan ravi
2020-7-13
“I tried to run it with for loop and it worked just fine.”
What is the problem then??
Toan Nguyen
2020-7-13
回答(1 个)
Mahesh Taparia
2020-7-16
0 个投票
Hi
It seems you want to apply a function on each element of an array without applying for loop. In this case, create that function according to the input, i.e, give array as the function input and apply the whole computation on that array which finally returns a vector as functional output.
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!