writing a matrix without loop

Dear all I want to write a matrix that its arrays are dependent on i and j, could I write it without writing loops. my arrays are something like it a(i,j)=sin(2*pi*(i-1))/((sin(2*pi*(i-1))+sin(2*pi*(j-1))))). Thanks

 채택된 답변

Image Analyst
Image Analyst 2012년 12월 23일

2 개 추천

Use meshgrid:
m = 0:20;
n = 0:4;
[x y] = meshgrid(m, n);
a = sin(2*pi*y) ./ ((sin(2*pi*y) + sin(2*pi*x)))
imagesc(a);
set(gca, 'ydir', 'reverse');

댓글 수: 3

ss
ss 2012년 12월 26일
Image Analyst thanks for your useful response, but what should we do if a(i,j)=x(j)*(i-1) that x is a vector? thank you again
x = randi(9, [8 1]) % Random, sample data.
iMinus1 = 0:11
a = x * iMinus1
ss
ss 2012년 12월 26일
thanks for your fast and useful response

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

추가 답변 (1개)

Laura Proctor
Laura Proctor 2012년 12월 23일

1 개 추천

Yes, you can create this matrix without using a loop. Use array based mathematical expressions such as ./ and .*
a = sin(2*pi*(ii-1))./(sin(2*pi*(ii-1))+sin(2*pi*(jj-1)));

카테고리

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

태그

질문:

ss
2012년 12월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by