Make a matrix from equation of vectors

Hi guys!
I want to make a matrix out of my equation with two variables. To do this, i made two vectors and make it run trough a if loop. I googled the error and I tried to do it in different ways, but after two hours I thought i'd ask my first question on these forums :)
clear
alpha = 1:1:10
beta = 2:2:20
j=1;
while j<10
y(j) = 3*beta(j)-2.*alpha
j=j+1;
end
This gives the error: 'Subscripted assignment dimension mismatch.' (in the line of y(j)). When I change it to y{j}, to make it a set, everything does work. The idea should work I think, but I can't figure out how to code it.
Thanks! J

Answers (1)

Andrei Bobrov
Andrei Bobrov on 21 Nov 2015
Edited: Andrei Bobrov on 21 Nov 2015
alpha = 1:1:10;
beta = 2:2:20;
solution:
y = bsxfun(@minus,3*beta',2*alpha);
or
m = numel(alpha);
n = numel(beta);
y = zeros(n,m);
b = beta(:);
for jj = 1:m
y(:,jj) = 3*b-2.*alpha(jj);
end

2 Comments

Thank you for your quick reply! Your second solution gives one row with only zeroes so I guess something went wrong there. I types n+1 at the while and now it works :)
Since the actual formula in my homework is this a bit complicated I think i won't be able to make it with the bsxfun function.
zeta{j} = (((mu*Fzf)*(1 - lapda(j)).*(1 - (Er*(sqrt((lapda(j).^2) + (tan(alpha)).^2)))))./(2*sqrt(((Cxf.*lapda(j)).^2)+((Cyf.*tan(alpha)).^2))));
variant
lapda = lapda(:)';
l2 = lapda.^2;
ta2 = tan(alpha(:)).^2;
a = bsxfun(@times,(1 - lapda),1 - Er*sqrt(bsxfun(@plus,l2,ta2)));
b = sqrt(bsxfun(@plus,Cxf.^2.*l2,Cyf.^2.*ta2));
zeta = mu*Fzf/2*a./b;

Sign in to comment.

Asked:

on 21 Nov 2015

Edited:

on 21 Nov 2015

Community Treasure Hunt

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

Start Hunting!