solve the system ax = b for different b’s in matlab

Assuming a is a matrix and b is a vector, you can use the backslash operator to solve for x:

main.m
x = a \ b;
11 chars
2 lines

If you have multiple values of b that you want to solve for, you can use a loop:

main.m
n = size(b, 2);
x = zeros(size(a, 2), n);
for i = 1:n
    x(:,i) = a \ b(:,i);
end
83 chars
6 lines

This will result in a matrix x where each column is the solution for the corresponding column of b.

related categories

gistlibby LogSnag