function retuning the whole vector

I have a function that input a matrix and a vector
function s= solve(A, b)
for i=1:N
s(i)= something
end
end
I want it to retunr the whole vector, like if disp(Solve(something)) would output a vector !
Can I use the return command here ?

Answers (1)

That code outline already returns a whole vector. For example,
disp(mysolve(magic(6), [6;5;5;3;2;1]))
385 347 381 439 455 435
function s = mysolve(A,b)
for i = 1 : size(A,1)
s(i) = dot(A(i,:), b);
end
end

4 Comments

I don't get it, I am getting only the first value of the vector at the ans variable, I just tried disp also and it display the same one value, however your function return the whole vector but I don't yet understand the difference between the two, here is mine :
function s= Solve(A, b) %TODO argument control: A must have same size as b
N= size(b,2);
% triangulation
for k= 1:N-1
for i= k+1:N
c= A(i,k)/A(k,k);
b(i)= b(i)- c*b(k);
for j= k:N
A(i,j)= A(i,j)- c*A(k,j);
end
end
end
%
s(N)= b(N)/A(N,N);
for i= N-1:-1:1
sum= 0.0;
for j= i+1:N
sum= sum+ A(i,j)*s(j);
end
s(i)= (b(i)- sum)/A(i,i);
end
end
In the code structure you posted, your upper limit of your for loop is N, but N is not shown as having a value. In your actual code how are you defining the upper limit?
First line, N=size()
Is the order of filling the vector relevant?
You take size(b, 2) which is the number of columns in b. But suppose you were passed a column vector then the number of columns would be 1.

Sign in to comment.

Asked:

on 21 Feb 2021

Commented:

on 22 Feb 2021

Community Treasure Hunt

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

Start Hunting!