Hi,
I want to fill my Matrix in following way.
I have 2 vectors that are used by my Matrix. Say
VectorA = 1:10;
VectorB = 20:29;
And I want to use them to fill my matrix.
for m = 1:10
for n = 20:29
MatrixK = [sin(m),cos(n);cos(n).*sin(m),exp(m)];
% MatrixK will be used here
end
end
However, as you know, it is not efficient as these 2 for loops have to be run by 100x100 times. How can I do it efficiently? I am not sure I have fully defined my problem. Most likely, I will add sth.
Thanks

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2014년 6월 26일
편집: Andrei Bobrov 2014년 6월 26일

0 개 추천

A = 1:10;
B = 20:29;
[m,n] = ndgrid(A,B)
K = [sin(m),cos(n);cos(n).*sin(m),exp(m)];
s = size(K);
p = size(m);
q = s./p;
z0 = reshape(K,p(1),p(2),q(1),q(2));
MatrixK = reshape(permute(z0,[2 1 4 3]),s(1),[]);

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 6월 26일

편집:

2014년 6월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by