Nested For Loop Equation

조회 수: 8 (최근 30일)
Kevin Krone
Kevin Krone 2019년 9월 20일
댓글: thoughtGarden 2019년 9월 20일
I have an array (N) filled with the 25 prime numbers between 1 and one hundred.
And I need to build the equation
x = (((((( (n)^ 99/100 n-1)^ 99/100 )+ n-2)^ 99100 ) +n-3)^99/100 )....+ 1 ^99/100)
and apply to every number in the array.
For example, the 6th prime number is 13, the equation would be
x = ((((13^(99/100)) + 12)^(99/100) +11) ^(99/100) +10)^(99/100)....+1)^99/100 = 63.6
I know this is going to need some sort of nested for loops create the equation.
for i = size(NN,2) % For i = 25
NN(i);
for j = max(NN) % For j = 97
((NN(i)^(99/100))+NN(i)-j)^(99/100)
end
end
This is my initial attempt, any guidance or help would be greatly appreciated.

채택된 답변

thoughtGarden
thoughtGarden 2019년 9월 20일
편집: thoughtGarden 2019년 9월 20일
Tis quick and dirty, but this work...
clear;clc;
N = primes(100);
newVect = zeros(size(N));
for ii = 1:length(N)
tempSum = 0;
for jj = N(ii):-1:1
tempSum = (tempSum + jj)^(99/100);
end
newVect(ii) = tempSum;
end
disp(newVect)
Update: I changed the variables to match your OP. I also cleaned up by adding preallocation.
As a side note, this seems like something you would want to put into a function... JMHO.
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 9월 20일
MATLAB provides a function for this kind of operation: fold()
thoughtGarden
thoughtGarden 2019년 9월 20일
Thanks for tip about fold(). I've never heard of it, but it does seem useful, especially for this application.

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by