Matrix sum

1 次查看(过去 30 天)
Kevin
Kevin 2011-9-26
Hi,
I'm dealing with a condition in a while loop that I can't get it right. Here is the example:
Given the initial matrix of P=[2 3 8;1 -3 6;-2 -1 0], calculate the e^P = sum(P^k/k!) which k=[0:N-1]. Now the question is how can I implant the sum in matlab? Also, using a while loop, I want to address each element of matrix to meet a condition (e.g. each element of P bigger than 0.5) how can I do that?

回答(1 个)

Walter Roberson
Walter Roberson 2011-9-26
eP = zeros(size(P));
for k = 0:N-1
eP = eP + P^k ./ k!;
end
For your second question, you need to indicate what condition should be used to terminate the "while" loop. Detection of the first element which is not greater than 0.5 ? Detection that you have run out of elements? Which order do you want to visit the elements in, considering that P will be a 2D matrix and thus could be traveled randomly or by rows or by columns or by diagonals or by Knight moves...

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by