Matlab only calculates values for only certain inputs in a matrix.

조회 수: 4 (최근 30일)
I have a simple function that calculates the inverse for each of the values in an array. When i invoke the function and pass in the array, the response array only has values that were calculated for a few of the values of the array that was passed into the function. My function is
function convof= inverseof(x)
for i=1:100
convof = 1/x
end
end
x=rand(100,1)
y=inverseof(x)
x =
0.778802241824093
0.423452918962738
0.0908232857874395
0.266471490779072
0.153656717591307
0.281005302533871
0.440085139001721
0.527142741760652
0.457424365687674
0.875371598604185
0.518052108361104
0.943622624548388
0.637709098072174
0.957693939841583
0.240707035480160
0.676122303863752
0.289064571674477
0.671808165414215
0.695140499551737
0.0679927684700106
0.254790156597005
0.224040030824219
0.667832727013717
0.844392156527205
0.344462411301042
0.780519652731358
0.675332065747000
0.00671531431847749
0.602170487581795
0.386771194520985
0.915991244131425
0.00115105712910724
0.462449159242329
0.424349039815375
0.460916366028964
0.770159728608609
0.322471807186779
0.784739294760742
0.471357153710612
0.0357627332691179
0.175874415683531
0.721758033391102
0.473485992965320
0.152721200438232
0.341124607049109
0.607389213768347
0.191745255461798
0.738426839976942
0.242849598318169
0.917424342049382
0.269061586686018
0.765500016621438
0.188661976791491
0.287498173066131
0.0911134636865350
0.576209380663007
0.683363243294653
0.546593114590323
0.425728841871188
0.644442781431337
0.647617630172684
0.679016754093202
0.635786710514084
0.945174113109401
0.208934922426023
0.709281702710545
0.236230576993797
0.119396247797306
0.607303940685635
0.450137696965896
0.458725493648868
0.661944751905652
0.770285514803660
0.350218013441105
0.662009598359135
0.416158589969797
0.841929152691309
0.832916819075216
0.256440992229147
0.613460736812875
0.582249164527227
0.540739337124410
0.869941032358007
0.264779026475630
0.318074075481059
0.119214541054191
0.939829470344921
0.645551874972524
0.479463224948888
0.639316961040108
0.544716110526763
0.647311480293128
0.543885933999639
0.721046620579811
0.522495305777102
0.993704624120852
0.218676632399634
0.105798273250228
0.109697464523194
0.0635913709751057
y =
Columns 1 through 9
0 0 0 0 0 0 0 0 0
Columns 10 through 18
0 0 0 0 0 0 0 0 0
Columns 19 through 27
0 0 0 0 0 0 0 0 0
Columns 28 through 36
0 0 0 0 0 0 0 0 0
Columns 37 through 45
0 0 0 0 0 0 0 0 0
Columns 46 through 54
0 0 0 0 0 0 0 0 0
Columns 55 through 63
0 0 0 0 0 0 0 0 0
Columns 64 through 72
0 0 0 0 0 0 0 0 0
Columns 73 through 81
0 0 0 0 0 0 0 0 0
Columns 82 through 90
0 0 0 0 0 0 0 0 0
Columns 91 through 99
0 0 0 0 0 1.0063 0 0 0
Column 100
0
surprisingy, here the 1.0063 value is correct which is the result of 1/0.993704624120852 i.e the 96th value of x. So not sure what to do here.

채택된 답변

John D'Errico
John D'Errico 2021년 9월 14일
편집: John D'Errico 2021년 9월 14일
Except that is not how to use a loop. You want to compute the inverse of each value in the array.
function convof= inverseof(x)
convof = zeros(size(x));
for i=1:100
convof(i) = 1/x(i);
end
end
What MATLAB actually did compute gets into linear algebra, and is arguably beyond this discusssion.

추가 답변 (1개)

the cyclist
the cyclist 2021년 9월 14일
Several thoughts:
First
When you write
convof = 1/x
you need to realize that x is a vector. I think you intended
for i=1:100
convof(i) = 1/x(i)
end
which would take the reciprocal of each element. The way you wrote it, you are not taking the reciprocal, because x is a vector. You are actually solving a system of linear equations.
Second
Rather than running your for loop from 1:100, you should run it over the length of x:
for i=1:length(x)
convof(i) = 1/x(i)
end
Third
You are not taking advantage of the power of MATLAB to do vectorized calculations. You could have done your entire code without the function, simply writing
x=rand(100,1)
y=1./x; % This is equivalent to your inverseof function
Fourth
You might benefit from the MATLAB Onramp tutorial to learn the basics better.

카테고리

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

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by