If statement for 2 different variables.
Show older comments
I have an equation:
x = [10 / pi + j / sin(i-1)] * cos[j * (i-1) * pi]
where i = 2 to 6 and j = 1 to 7. I am trying to make a code with both i and j. I am solving this equation for all combinations. For example, I want to solve the equation when i = 2 and j = 1, 2, ..., 7. And when i = 3, j = 1, 2, ..., 7. The end result should also be put in a matrix i by j = 5 by 7.
Thank you to all that reply!
3 Comments
Dyuman Joshi
on 22 Sep 2023
Look into the 1st example provided in the documentation of for loop
Kavi Patel
on 22 Sep 2023
Star Strider
on 22 Sep 2023
Answers (1)
Try this —
x = @(i,j) (10 / pi + j ./ sin(i-1)) .* cos(j .* (i-1) * pi);
i = 2:6;
j = 1:7;
[I,J] = ndgrid(i,j);
Iv = I(:);
Jv = J(:);
Xv = x(Iv,Jv);
X = x(I,J) % Matrix
figure
surf(I,J,X) % Surface Plot
grid on
colormap(turbo)
xlabel('i')
ylabel('j')
zlabel('x(i,j)')
ResultVectors = table(Iv,Jv,Xv) % Table Of Vectors
.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!