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!

Respuestas (1)

Guillaume
Guillaume el 17 de Nov. de 2016
Editada: Guillaume el 17 de Nov. de 2016
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 comentarios

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 el 17 de Nov. de 2016
Editada: Guillaume el 17 de Nov. de 2016
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);

Iniciar sesión para comentar.

Preguntada:

el 17 de Nov. de 2016

Editada:

el 17 de Nov. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by