フィルターのクリア

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

2 ビュー (過去 30 日間)
Beibit Sautbek
Beibit Sautbek 2016 年 8 月 2 日
コメント済み: Star Strider 2016 年 8 月 2 日
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 日
Beibit Sautbek’s ‘Flag’ moved here:
Values are different
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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by