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
KSSV 2020년 7월 13일
What is ComplexIntegral?
madhan ravi
madhan ravi 2020년 7월 13일
Without seeing the function , how can a solution be suggested?
Toan Nguyen
Toan Nguyen 2020년 7월 13일
편집: Toan Nguyen 2020년 7월 13일
My bad. Here it is.
function [result] = Fx(w)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
load hw11.mat
%load the audio file with Fs = 44100 and the audio matrix "toan"
toan = reshape(toan,[1,115824]);
%reshape the 1115824x1 to 1x115824
n = 1:length(toan);
t = timearray(n);
%Create an array of t which is 1x115824
a = audio(n);
%Create an array of a which is 1x115824
sgma = zeros(1,length(n));
%Create sigma array 1x115824
e = exp(1)*ones(1,length(n));
%Create exponential array 1x115824
omega = -(sgma +j*w);
%Create omega array 1x115824
c = a.*e.^(omega.*t);
%Element wise calculation. The c matrix is 1x115824
result = sum(c);
%Sum of all element of c.
end
madhan ravi
madhan ravi 2020년 7월 13일
Did you use w inside the function?
Toan Nguyen
Toan Nguyen 2020년 7월 13일
Yes I did.
I create the omega array with w: omega = -(sgma + j*w);
sgma is the 1x115824 matrix.
r = cell(10, 1); % before loop
r{n} = ... % inside loop the rest remains unchanged
Toan Nguyen
Toan Nguyen 2020년 7월 13일
Thank you for your help.
Could you elaborate on what happened in your code so I can look into it.
Also, what do you mean by "before loop" and "inside loop"? I don't have "for loop" nor "while loop" in my code. Are you refering to the Function file?
Thank you
madhan ravi
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
Toan Nguyen 2020년 7월 13일
I'm sorry that I did not clarify the objective.
For loop is slow with 100 runs already and my aim is around 100k.
From what I heard, vectorization is faster. Thus, that's what I'm trying to do

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

답변 (1개)

Mahesh Taparia
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에 대해 자세히 알아보기

질문:

2020년 7월 13일

답변:

2020년 7월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by