필터 지우기
필터 지우기

Sum of matrix with variable dimension

조회 수: 1 (최근 30일)
Favier
Favier 2016년 10월 29일
댓글: Favier 2016년 10월 30일
Hello, I have two matrices produced by meshgrid and a simple scalar:
r = 1:10;
N = 1:10;
[r,N] = meshgrid(r,N);
N0 = 4;
I want, for each value of N, to get the sum from N0 to N of r^n. However this Matlab commands is not working :
sum(r.^(N0:N));
For example for r(5,5) = 5 and N(5,5) = 5, the result would be 5^4 + 5^5 = 3750. What I want is to get the matrix resultant of this operation over each row and column of r and N.
Thank you for your help!
  댓글 수: 2
Image Analyst
Image Analyst 2016년 10월 29일
Why is the N value raised to the 5th power while the r value is raised to the 4th power? What does it mean to have an exponent that is a vector, like [4,5]?
Favier
Favier 2016년 10월 29일
Thank you for your interest. Sorry I should have detailed my example a bit more. When I write 5^4 + 5^5 = 3750, it should be read as r(5,5)^N0 + r(5,5)^N(5,5) = 3750.
If N0 = 4 and N(5,5) = 7, then I want to calculate r(5,5)^N0 + r(5,5)^(N0+1) + r(5,5)^(N0+2) + r(5,5)^(N(5,5))
The exponent is a vector to simplify the representation. What I want is to calculate the sum of r^n with n from N0 to N for ALL my r and ALL my N. The larger the N, the more terms I have in the sum. That is the purpose of the vector exponent.

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

채택된 답변

Guillaume
Guillaume 2016년 10월 29일
I'm not sure what should happen when N(y,x) < N0, should the result be 0?
If I understand correctly, this will do:
arrayfun(@(rr, nn) sum(rr .^(N0:nn)), r, N)
Note that the above works for N(y, x) < N0 because the sum of an empty vector is 0 instead of empty.
  댓글 수: 1
Favier
Favier 2016년 10월 30일
The result should indeed be zero when N < N0. I like your solution a lot.
Thank you very much!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by