how to loop in dynamic dimension matrix?

1 次查看(过去 30 天)
How could i loop in dynamic dimension array?
Currently i'm using switch-case statement, but the code looks redundant, and it can only support limited dimension.
function mode_vec = gen_mode_vec(freq,signal_size)
mat=zeros(signal_size);
dimension=length(signal_size);
switch dimension
case 1
for m=1:signal_size(1)
mode_vec(m)=exp(1i*dot(m,freq));
end
case 2
for m=1:signal_size(1)
for n=1:signal_size(2)
mode_vec(m,n)=exp(1i*dot([m,n],freq));
end
end
case 3
for m=1:signal_size(1)
for n=1:signal_size(2)
for k=1:signal_size(3)
mode_vec(m,n,k)=exp(1i*dot([m,n,k],freq));
end
end
end
case 4
for m=1:signal_size(1)
for n=1:signal_size(2)
for k=1:signal_size(3)
for p=1:signal_size(4)
mode_vec(m,n,k,p)=exp(1i*dot([m,n,k,p],freq));
end
end
end
end
otherwise
fprintf('only support dimension 1-4\n' );
end
mode_vec=reshape(mat,[],1);
end
  1 个评论
Walter Roberson
Walter Roberson 2019-8-26
Is that dot() the dot product function, or is it an array, or is it a different function?

请先登录,再进行评论。

回答(0 个)

类别

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