how to write in matlab
Show older comments
hi, i am having c code how to write the below code in matlab
for (i = 0 ; i < n; i++)
scanf("%d", &num[i]);
for (j = 1; j <= n; j++) {
for (i = 0; i < n-1; i++) {
temp = num[i];
num[i] = num[i+1];
num[i+1] = temp;
print(num, n);
}
}
Answers (4)
KALYAN ACHARJYA
on 4 Feb 2019
%Please note I did not check the logic of the code
for i=1:n
check here sscanf
for j=1:n
for i=1:n
temp=num(i);
num(i)=num(i+1);
num(i+1)=temp;
fprintf('%d %d',num,n)
end
end
end
3 Comments
Luna
on 4 Feb 2019
looks like circshift isn't it?
KALYAN ACHARJYA
on 4 Feb 2019
Edited: KALYAN ACHARJYA
on 4 Feb 2019
@Luna yes. It seems so. Hello @msr16 for circshift, read here and implement, any problem let me know here.
Rose May Hidalgo
on 8 Jan 2021
0 votes
B=3*eye(size)A)-A)
1 Comment
Walter Roberson
on 4 May 2022
B = 3 * (eye(size(A)) - A)
Renesh Dayah
on 4 May 2022
0 votes
create variable for x for (-π,π)
1 Comment
For the most part, it is not possible to create a variable in MATLAB that is restricted to a certain range -- not without creating your own object oriented class and being willing to use strict rules about how to assign values to the variables.
You can create symbolic variables and give the symbolic engine assumptions about the range, in a way that in some cases the symbolic engine can reason about the value
syms x
Pi = sym(pi);
assume(x > -Pi & x < Pi)
assumptions
isAlways( imag(sin(x)) == 0)
In some cases you can configure optimization variables to be in a particular range; see https://www.mathworks.com/help/optim/ug/optimconstr.html
Dhruv
on 23 Oct 2023
0 votes
sin(pi/4)+e^-3/2
1 Comment
Walter Roberson
on 23 Oct 2023
The sin(pi/4) part can be written as
sin(pi/4)
The e^ part should be written as exp
exp(-3/2)
Note however, that instead of sin(pi/4) you get better precision if you write
sinpi(1/4)
Categories
Find more on Assumptions 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!