Matrix Output Matlab Calculation

In summary, the conversation is about creating a script-m file that produces a matrix output for a formula. The person is having trouble getting the script to stop at a certain 'i' value and solve for all 'j' values before proceeding. The expert suggests using nested for loops to iterate through both 'i' and 'j' values and construct a matrix with two dimensions to solve for all combinations.
  • #1
Air
203
0
I am making a script-m file to produce results of a formula in a Matrix output.

Code:
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.
 
Physics news on Phys.org
  • #2
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:

Code:
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
 
  • #3


As a scientist, it is important to thoroughly consider all possible combinations in your calculations. In this case, you can use a nested for loop to iterate through all combinations of 'i' and 'j' values. This will ensure that all possible combinations are considered and the corresponding 'k' values are calculated before moving on to the next 'i' value. The updated code could look something like this:

function s = calc(a, b)

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

k = zeros(length(i),length(j)); %initialize k matrix with appropriate dimensions

for m = 1:length(i) %loop through all 'i' values
for n = 1:length(j) %loop through all 'j' values for each 'i' value
k(m,n) = i(m)^2 + j(n)*i(m); %calculate corresponding 'k' value
end
end

A = [i' j' k']; %create matrix with 'i', 'j', and 'k' values

disp('i j k'), disp(A) %display matrix

I hope this helps you achieve the desired output for your script-m file.
 

1. What is "Matrix Output" in Matlab Calculation?

Matrix Output in Matlab Calculation refers to the result of a computational operation where the output is a matrix. Matrices are rectangular arrays of numbers that can be manipulated mathematically using various operations such as addition, subtraction, multiplication, and division. In Matlab, matrices are the primary data type used for performing mathematical calculations and storing the results.

2. How do I perform Matrix Output in Matlab Calculation?

To perform Matrix Output in Matlab Calculation, you need to first define the matrices involved in the calculation. This can be done by using the "matrix" function or by manually entering the values into the command window. Once the matrices are defined, you can use various built-in functions and operators to perform the desired operations on the matrices and obtain the resulting matrix output.

3. What is the difference between Matrix Output and Scalar Output in Matlab Calculation?

The main difference between Matrix Output and Scalar Output in Matlab Calculation is the type of output obtained. As the name suggests, Matrix Output results in a matrix as the output, whereas Scalar Output results in a single value (a scalar) as the output. This is because Matrix Output involves operations on multiple values, while Scalar Output involves operations on a single value.

4. Can I save the Matrix Output from Matlab Calculation to a file?

Yes, you can save the Matrix Output from Matlab Calculation to a file using the "save" function. This function allows you to specify the name of the file and the variables that you want to save. The saved matrix can then be loaded into Matlab for further analysis or used in other programs.

5. How do I display the Matrix Output in a more readable format?

To display the Matrix Output in a more readable format, you can use the "disp" function. This function displays the values of the matrix output in a tabular format, making it easier to read and understand. Additionally, you can also use the "format" function to specify the desired format for the output, such as the number of decimal places or the use of scientific notation.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
756
Back
Top