Seeking guidance on how to do programming for function and matrix

How do I program ac-b^2, given that a and c are both all numbers from 1 to 10 and b is all numbers from -10 to 10? I have been searching hours for an answer. I finally did it long hand and got 986 possible matrix combinations for [a b;b c] that fit. I was introduced to Matlab for the very first time about 3 weeks ago, so I am woefully undereducated on it. Any assistance is appreciated. Thanks!

回答 (1 件)

Guillaume
Guillaume 2016 年 11 月 17 日
編集済み: Guillaume 2016 年 11 月 17 日

1 投票

I assume all numbers from 1 to 10 actually means all integers from 1 to 10, otherwise there's an infinity of numbers...
In R2016b, it's dead easy to get all combinations. Simply have all vectors along different dimension and implicit expansion will take care of the rest:
a = 1:10; %vector along 1st dimension (row vector)
b = (-10:10).'; %vector along 2nd dimension (column vector)
c = permute(1:10, [1 3 2]); %vector along 3rd dimension
result = a.*c - b.^2
Prior to R2016b, you have to use bsxfun for that last line:
result = bsxfun(@minus, bsxfun(@times, a, c), b.^2);

2 件のコメント

Marisol Riddell
Marisol Riddell 2016 年 11 月 17 日
Thank you for your help, Guillaume I cut and pasted what you wrote, but I got this message in return:
Error using .* Matrix dimensions must agree.
Maybe I'm missing something?
Guillaume
Guillaume 2016 年 11 月 17 日
編集済み: Guillaume 2016 年 11 月 17 日
Clearly, you're not using R2016b. As I wrote:
Prior to R2016b, you have to use bsxfun for that last line:
result = bsxfun(@minus, bsxfun(@times, a, c), b.^2);

サインインしてコメントする。

質問済み:

2016 年 11 月 17 日

編集済み:

2016 年 11 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by