Question

In: Computer Science

I am learning Matlab. I have a math formula such as A(x , y)= (x+1) /...

I am learning Matlab.

I have a math formula such as A(x , y)= (x+1) / (y+1), and n=6

How can I write down the for loop in Matlab to express 1 <= x and y < = n?

Thanks

Solutions

Expert Solution

Use of for loop is important aspect and we must also learn applying condtions.

See the code below that will help you understand concepts:

n= 5; 
A = zeros(n);% declare a n*n matrix with all values 0

% Now let's fill value using 2 loops i and j

for i = 1:n  % loop works for i = 1,2,3 ... n-1,n
    for j = 1:n
        A(i,j) = (i+1)/(j+1);
    end
end % make sure to write end 

disp(A); % prints in console window

%see how to apply conditions
% let's set all the values greater than 1 to 200 with i = 2 or 4
for i = 1:n
    for j=1:n
        % see how to use OR , AND condition and if condition
        % we want( i = 2 or i =4) and A(i,j)>1
        if(((i == 2)||(i==3))&&(A(i,j)>1))
            A(i,j) = 200;
        end
    end
end
disp(A);

Code screenshot:

Code output:

===============

Explanation :

The if condition is generally used to check certain conditions and based on them it executes the statement below it. t can be followed by else part, which is executed when if condition fails.

Here is the same code that uses while loop instead of for loop. It is also important to understand while loop working.

n= 5; 
A = zeros(n);% declare a n*n matrix with all values 0

% Now let's fill value using 2 loops i and j
i=1;
while(i<=n)  % loop till i<n
    j = 1;
    while(j<=n)
        A(i,j) = (i+1)/(j+1);
        j=j+1; % increment j to move to next column
    end
    i = i+1;
    % increment i to move to next row
end % make sure to write end 

disp(A); % prints in console window

Code Screenshot:

Code output:

Note that they have same output. We just performed the same task using while loop instead of for loop.

If you still have any doubts please comment.


Related Solutions

y'=y-x^2 ; y(1)= -4 My MATLAB program won't work. I am trying to get the main...
y'=y-x^2 ; y(1)= -4 My MATLAB program won't work. I am trying to get the main program to output a plot of the three equations (1 from the main program and two called in the function). The goal is to code a Euler method and a 2nd order Taylor numerical solution for a. x0= 1.0 , step size h= 0.2, # of steps n=20 b. x0= 1.0 , step size h=0.05 , # of steps n=80 ; write a separate...
How is this formula for βˆ 1 related to the formula for cov(X, Y )  Express the...
How is this formula for βˆ 1 related to the formula for cov(X, Y )  Express the formula for βˆ 1 as a function of the cov(X, Y )
No excel please! I am trying to learn the math for Exam! Thank yoU! You have...
No excel please! I am trying to learn the math for Exam! Thank yoU! You have just joined the investment banking firm of Mckenzie & Co. They have offered you two different salary arrangements. You can have $75,000 per year for the next two years, or you can have $55,000 per year for the next two years, along with a $30,000 signing bonus today. If the interest rate is 12% compounded monthly, which is a better offer? NB: first convert...
let the continuous random variables X and Y have the joint pdf: f(x,y)=6x , 0<x<y<1 i)...
let the continuous random variables X and Y have the joint pdf: f(x,y)=6x , 0<x<y<1 i) find the marginal pdf of X and Y respectively, ii) the conditional pdf of Y given x, that is fY|X(y|x), iii) E(Y|x) and Corr(X,Y).
I am trying to do edge detection using matlab. I have posted code here. It does...
I am trying to do edge detection using matlab. I have posted code here. It does not give any error but it's not running it at all. So please help me to fix it and also exaplin each line of this code pls. Thanks! i = imread('C:\Users\Amanda\Desktop"); I = rgb2gray(1i); BW1 = edge(I,'prewitt'); BW2= edge(I,'sobel'); BW3= edge(I,'roberts'); subplot (2,2,1); imshow(I); title('original'); subplot(2,2,2); imshow(BW1); title('Prewitt'); subplot(2,2,3); imshow(BW2); title('Sobel'); subplot(2,2,4); imshow(BW3); title('Roberts');
Consider the formula A : ∃x.[(∀y.P(x, y) → R(x)) → ¬∃z.Q(x, z)] (a) Find a formula...
Consider the formula A : ∃x.[(∀y.P(x, y) → R(x)) → ¬∃z.Q(x, z)] (a) Find a formula equivalent to A that only has negation symbols in front of basic formulas. (b) Give an example of an interpretation where A is true. The domain should be the set N. (c) Give an example of an interpretation where A is false. The domain should be the set N.
I am very lost on this. If there is an Excel formula I could use, that...
I am very lost on this. If there is an Excel formula I could use, that would help tremendously. Thank you... Create confidence intervals related to the interval and ratio-level data you collected. 1. What is the best estimate of the population mean 2. Develop a 95% confidence interval for the population mean. Develop a 90% confidence interval for the population mean. Develop a 98% confidence interval for the population mean. 3. Interpret the confidence interval. Create an individual Excel...
This is Using MATLAB: I am trying to store the solution of this matrix because I...
This is Using MATLAB: I am trying to store the solution of this matrix because I want to do something with the result like find the norm of that answer however I am stuck and cannot seem to be able to. Help would be appreciated! --------------------------------------------- MATLAB CODE: close all clear clc A = [1 -1 2 -1; 2 -2 2 -3; 1 1 1 0; 1 -1 4 5]; b = [-8 -20 -2 4]'; x = gauss_elim(A,b) function...
*(1)(a) Find a formula for the intersection of a cone {(x,y,z): x^2+y^2=z^2} with a plane {(x,y,z):...
*(1)(a) Find a formula for the intersection of a cone {(x,y,z): x^2+y^2=z^2} with a plane {(x,y,z): z=c}. (b) Find a formula for the intersection of a cone {(x,y,z): x^2+y^2=z^2} with a plane {(x,y,z): x=a}. (c) Find a formula for the intersection of a cone {(x,y,z): x^2+y^2=z^2} with a plane {(x,y,z): y=b}. *(2) Find a formula for the intersection of a cone {(x,y,z): x^2+y^2=z^2} with a plane {(x,y,z): z=kx+b} assuming both b and k are positive. (a) For what value of...
y'=y-x^2 ; y(1)= -4 Write a MATLAB program that makes two plots of the solution to...
y'=y-x^2 ; y(1)= -4 Write a MATLAB program that makes two plots of the solution to the equation using the following values. Suggest you use nested loops instead of two different loops. Be sure to label your plots. a. x0 = 1.0, step size h = .2, number of steps n = 20. b. x0 = 1.0, step size h = .05, number of steps n = 80.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT