# Converting Matlab code to Mathematica [closed]

I have following Matlab code:

xC= zeros(1,n);
yC= zeros(1,n);

for i=1:(b/delta);
xC(i) = -a/2;
yC(i) = b-(i-1/2)*delta;
end;
for i=1:(a/delta);
xC(i+b/delta) = -a/2+(i-1/2)*delta;
yC(i+b/delta) = 0;
end;
for i=1:(b/delta);
xC(i+b/delta+a/delta) = a/2-(1/2)*delta;
yC(i+b/delta+a/delta) = (i-1/2)*delta;
end;


Since i'm new to mathematica can anyone help me convert this to mathematica code.

explanation: I am trying to create a matrices 1x300 (n=300).

creating empty matrices with this:

xC= zeros(1,n);
yC= zeros(1,n);


First For loop is filling first 50 elements of matrices with values -a/2 and b-(i-1/2)*delta. Second loop is filling the next 200 elements of matrices with values -a/2+(i-1/2)*delta and 0 and the third loop is filling last 50 elements with a/2-(1/2)*delta and (i-1/2)*delta.

What i have tried with mathematica is this:

bdelta = b/delta
adelta = a/delta

xC = Table[0, {i, n}]
yC = Table[0, {i, n}];

xC = Table[-a/2, {i, bdelta}];
yC = Table[b - (i - 1/2)*delta, {i, bdelta}];

xC = Table[-a/2 + (i - 1/2)*delta, {i, bdelta + 1, adelta + bdelta}];
yC = Table[0, {i, bdelta + 1, bdelta + adelta}];

xC = Table[a/2 - (1/2)*delta, {i, bdelta + adelta + 1, n}];
yC = Table[(i - 1/2)*delta, {i, bdelta + adelta + 1, n}];


since it's not working and i have very little experience with mathematica and its syntax i would really need some help with this.

• The first for loop is xC = PadRight[ConstantArray[-a/2, Floor[b/delta]], n]; and yC = PadRight[b - (Range[b/delta] - 1/2) delta, n];. In the second for loop you write yC(i+b/delta) = 0; but these are already equal to 0 aren't they?! – Coolwater Mar 14 '18 at 12:18
• Have you tried anything? The idea is that OP should prove a minimal familiarity with Mathematica. Additionally, everything that does not concern MMA should be explained. I don't think zeros(1,n) is obvious. – Kuba Mar 14 '18 at 12:38
• – Michael E2 Mar 14 '18 at 14:00
• Your Matlab code doesn't run in Matlab. – MikeY Mar 4 '19 at 14:56
• I'm voting to close this question as off-topic because it is too localized; i.e, it applies only to the local situation and needs of its poster. Answers will not benefit others. – m_goldberg Mar 4 '19 at 17:45

## 1 Answer

Less an answer than an extended comment.

You will get far greater response from this community it you show some Mathematica code that you've tried.

Additionally, you can go to MATLAB's website and determine what MATLAB's functions and syntax do.

From that, start looking at Mathematica's built-in documentation or simply search the Internet.

Much of MATLAB's syntax operates contextually, e.g., "()" have a number of different uses. I think you'll find Mathematica far more specific.

If memory serves, MATLAB could view the n in zeros(1, n) in any of a few ways, e.g. as :

• function
• class name
• variable or number
• scalar
• vector
• matrix

If you run zeros(1, n) in MATLAB it returns:

Undefined function or variable 'n'.

So to give you a Mathematica equivalent, we need additional information.

What output do you want from zeros(1, n)?

• vector?
• matrix?
• other?

That said, a Mathematica equivalent might look like this:

Table[0, 10]
Table[0*i, {i, 10}]
Table[0*i, {i, 10}, {i, 10}]


A couple of the above likely being silly.

I just can't "know" want you want to accomplish from the question so far.

@gwr in his comment to your answer makes a great recommendation to look at:

Alternatives to procedural loops and iterating over lists in Mathematica

@coolwater's recommendations:

xC = PadRight[ConstantArray[-a/2, Floor[b/delta]], n];
yC = PadRight[b - (Range[b/delta] - 1/2) delta, n];


might work, but they seem to make assumptions about the variables.

Of course the loops themselves may have a context that @coolwater understands that I don't.

In short, give us some more information and try some things on your own. Get specific about where you get stuck.

Welcome to the forum.