Register to reply

Matrix Output (Matlab)

by Air
Tags: matlab, matrix, output
Share this thread:
Air
#1
Mar16-09, 06:20 AM
P: 206
I am making a script-m file to produce results of a formula in a Matrix output.

function s = calc(a, b)

i = [-a: 1: a];
j = [-b: 1: b];

k = i.^2 + j.*i;

A = [i' j' k']

disp('i  j  k'), disp(A)
But, it does not consider all combination. How can I make it stop at a 'i' value so all 'j' values can be matched to that and solved for 'k' before proceeding. In my code, it just does one round of 'i' and 'j' values.
Phys.Org News Partner Science news on Phys.org
Physical constant is constant even in strong gravitational fields
Montreal VR headset team turns to crowdfunding for Totem
Researchers study vital 'on/off switches' that control when bacteria turn deadly
KLoux
#2
Mar16-09, 03:08 PM
P: 175
I'm not sure I understand the question...

If you want to create a matrix k that contains every combination of the i and j values, then you might try something like:

function s = calc(a, b)

i = [-a: 1: a];
j = [-b: 1: b];

%  Use an outer for loop to iterate through all of the i values (uses index u)
for u = 1:1:max(size(i))
    %  Use an inner for loop to iterate through all of the j values (uses index v)
    for v = 1:1:max(size(j))
        %  Here we construct k to have two dimensions, the first corresponding to
        %  the i value, the second corresponding to the j value
        k(u, v) = i(u)^2 + j(v) * i(u);
    end
end

%  If i and j are different lengths, MATLAB won't let you do this...
A = [i' j' k']

disp('i  j  k'), disp(A)
Hope this helps,

Kerry


Register to reply

Related Discussions
(MATLAB) Ignoring output arguments Math & Science Software 6
Squareing Matrix(?) in MATLAB Math & Science Software 5
Matlab - matrix expressions Math & Science Software 0
Matrix of ODE's in MATLAB Math & Science Software 2
MATLab matrix question Math & Science Software 1