Please help me out. This code isn't working...
qq=8:10008;
for m = 1:9
for n = 1:10001
A(m, n) = (m+n)+qq;
end
end
I just want answer for A

댓글 수: 3

Adam
Adam 2019년 8월 8일
편집: Adam 2019년 8월 8일
No idea what answer you want since you didn't tell us and you already said the code isn't working so there's not much point us looking at that to understand what you want.
However, you are adding a vector, qq, to a scalar (m+n) and trying to assign the result of this to a scalar A(m,n) which clearly will not work. At a guess you want to add qq(n) instead.
DARLINGTON ETAJE
DARLINGTON ETAJE 2019년 8월 8일
You just solved the problem...qq(n) works....thank you.
Alex Mcaulley
Alex Mcaulley 2019년 8월 9일
편집: Alex Mcaulley 2019년 8월 9일
You can do it without loop. For example:
qq = 8:10008;
n = 1:10001;
m = 1:9;
[X,Y] = meshgrid(qq + n,m)
A = X + Y;
If you use the loops, at least preallocate your array A to minimize the execution time:
qq = 8:10008;
A = zeros(9,10001);
for m = 1:9
for n = 1:10001
A(m, n) = (m+n)+qq(n);
end
end

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

답변 (1개)

Jos (10584)
Jos (10584) 2019년 8월 9일

1 개 추천

In recent ML versions there is no need for meshgrid or so. The plus syntax will expand the vectors :-)
% a smaller example
n = 1:11
m = 1:4
q = 8:18
A = m' + n + q

카테고리

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

질문:

2019년 8월 8일

답변:

2019년 8월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by