Why are these zeros adding themselves to the array?

조회 수: 4 (최근 30일)
Andrew
Andrew 2025년 2월 6일
댓글: Voss 2025년 2월 6일
clear
clc
function result = expn(x,n)
result = 1;
for i = 1:n
result = (result + x.^i/factorial(i));
end
end
x = [1,-2];
n = [1,2,4,6,10];
ex1 = zeros(1,5);
ex_2 = zeros(1,5);
for k = n
t = expn(x(1),k);
ex1(k) = t;
end
for k = n
t = expn(x(2),k);
ex_2(k) = t;
end
ex1
ex1 = 1×10
2.0000 2.5000 0 2.7083 0 2.7181 0 0 0 2.7183
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ex_2
ex_2 = 1×10
-1.0000 1.0000 0 0.3333 0 0.1556 0 0 0 0.1354
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Not sure why the zeros are appearing as elements in the arrays. If someone could help me out it would be much appreciated!
  댓글 수: 2
Torsten
Torsten 2025년 2월 6일
편집: Torsten 2025년 2월 6일
The zeros are at the positions that are not covered by n = [1,2,4,6,10];. Would you prefer a number different from 0 to be set there ?
Andrew
Andrew 2025년 2월 6일
Oh that makes sense. No I'd rather there be no zeros, and have the array consist only of the nonzero numbers.

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

채택된 답변

Voss
Voss 2025년 2월 6일
편집: Voss 2025년 2월 6일
k goes 1,2,4,6,10
n = [1,2,4,6,10];
for k = n
fprintf('k = %d\n',k);
end
k = 1 k = 2 k = 4 k = 6 k = 10
so inside your loop, e.g., ex1(k) = t; sets ex1(1), ex1(2), ex1(4), ex1(6), ex1(10)
When you set an element of an array that's outside the current size of the array, the array is expanded as necessary with elements containing zeros. So that's where the zeros are coming from.
Example:
vec = zeros(1,5)
vec = 1×5
0 0 0 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
vec(4) = 40 % not expanded
vec = 1×5
0 0 0 40 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
vec(10) = 100 % vec gets expanded to length 10 with zeros
vec = 1×10
0 0 0 40 0 0 0 0 0 100
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
You probably meant for ex1 and ex_2 to be length-5 vectors for their entire lifetimes, in which case you'd do something like this:
x = [1,-2];
n = [1,2,4,6,10];
ex1 = zeros(1,5);
ex_2 = zeros(1,5);
m = numel(n);
for k = 1:m
t = expn(x(1),n(k));
ex1(k) = t;
end
for k = 1:m
t = expn(x(2),n(k));
ex_2(k) = t;
end
  댓글 수: 2
Andrew
Andrew 2025년 2월 6일
Yes this is exactly what I meant! Thank you very much!
Voss
Voss 2025년 2월 6일
You're welcome!

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2025년 2월 6일
function result = expn(x,n)
result = 1;
for i = 1:n
result = (result + x.^i/factorial(i));
end
end
x = [1,-2];
n = [1,2,4,6,10];
ex1 = dictionary();
ex_2 = dictionary;
for k = n
t = expn(x(1),k);
ex1(k) = t;
end
for k = n
t = expn(x(2),k);
ex_2(k) = t;
end
ex1
ex1 = dictionary (double --> double) with 5 entries: 1 --> 2 2 --> 2.5000 4 --> 2.7083 6 --> 2.7181 10 --> 2.7183
ex_2
ex_2 = dictionary (double --> double) with 5 entries: 1 --> -1 2 --> 1 4 --> 0.3333 6 --> 0.1556 10 --> 0.1354

Catalytic
Catalytic 2025년 2월 6일
ex1=[2.0000 2.5000 0 2.7083 0 2.7181 0 0 0 2.7183]
ex1 = 1×10
2.0000 2.5000 0 2.7083 0 2.7181 0 0 0 2.7183
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
ex1=nonzeros(ex1)'
ex1 = 1×5
2.0000 2.5000 2.7083 2.7181 2.7183
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by