How to use values of certain matrices without using for loop?

2 次查看(过去 30 天)
I have certain values a, b and c. I have P=a*b/c;
For example, If a=2, b=6, c=4. Then P will be P=2*6/4=3;
But, If I want to check three cases, when values of a, b and c will change, how P will be changed?
First case:: If I want to check effect of 'a',
If a=(1:1:10); b=6, c=4. So, in this case P will be matrix .
Second case:: If I want to check effect of 'c',
If a=2; b=6, c=(100:100:1000). So, in this case P will be matrix .
How Can I do this, without using for loop and 'if'?
Could anyone help me?
P.S. I have large code, and a lot of values as a, b and c. Therefore will be very complicated to use for loop and if.

采纳的回答

Star Strider
Star Strider 2016-8-2
You need to vectorize the expression. The easiest way to do what you want is to turn ‘P’ into an anonymous function and if all your values are between 1 and 10, use a meshgrid call:
P = @(a,b,c) a.*b./c;
[M1,M2,M3] = meshgrid(1:10);
Pm = P(M1,M2,M3);
This runs. I do not know what you want, so I will leave you to sort out the ‘Pm’ matrix.
  2 个评论
Star Strider
Star Strider 2016-8-2
Change the meshgrid call:
a_range = [a1, a2];
b_range = [b1, b2];
c_range = [c1, c2];
av = linspace(min(a_range), max(a_range), 10);
bv = linspace(min(b_range), max(b_range), 10);
cv = linspace(min(c_range), max(c_range), 10);
[M1,M2,M3] = meshgrid(av, bv, cv);
Change the number of vector elements (here 10) in each vector if necessary.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by