Lets assume we have an optimization problem like this:
x = zeros(1,10);
x(1,1) = 2;
for k = 1: 9
x(k+1) = 10 x(k);
end
Is it possible to write the equation without the for loop?

 채택된 답변

John BG
John BG 2017년 2월 14일

0 개 추천

Sakil
ok for a matrix, look:
A=[1 2;3 4];n=[1:1:10];L=A(:);B=reshape(bsxfun(@power,L,n),[size(A),10])
the resulting exponential matrices are stored in the layers of B
B(:,:,1) =
1 2
3 4
B(:,:,2) =
1 4
9 16
B(:,:,3) =
1 8
27 64
B(:,:,4) =
1 16
81 256
B(:,:,5) =
1 32
243 1024
B(:,:,6) =
1 64
729 4096
B(:,:,7) =
1 128
2187 16384
B(:,:,8) =
1 256
6561 65536
B(:,:,9) =
1 512
19683 262144
B(:,:,10) =
1 1024
59049 1048576
.
Sakil
would you please be so kind to mark my answer as Accepted Answer,
thanks in advance
regards
John BG

추가 답변 (1개)

John BG
John BG 2017년 2월 13일

1 개 추천

this
x = zeros(1,10);
x(1) = 2;
for k = 1: 9
x(k+1) = 10 *x(k);
end
x
=
Columns 1 through 3
2.00 20.00 200.00
Columns 4 through 6
2000.00 20000.00 200000.00
Columns 7 through 9
2000000.00 20000000.00 200000000.00
Column 10
2000000000.00
is the same as
n=[1:1:10];x=.2*10.^n
x =
Columns 1 through 3
2.00 20.00 200.00
Columns 4 through 6
2000.00 20000.00 200000.00
Columns 7 through 9
2000000.00 20000000.00 200000000.00
Column 10
2000000000.00
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance
John BG

댓글 수: 3

Hi. What if the multiplier is not 10? say the multiplier is a 2 by 2 matrix A. Then the statement will be:
x = x(1) * (A.^(0:9));
but this seems to be not working. Can you please check that?
More compactly,
x = 2 * 10.^(0:9);
Jan
Jan 2017년 2월 14일
@Sakil: The operation A.^(0:9) is not valid, therefore the readers have to guess, what you want as result. Which dimension does x have after this calculation?

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

카테고리

질문:

2017년 2월 13일

댓글:

Jan
2017년 2월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by