I need a help for MATLAB code problems!

3 Ansichten (letzte 30 Tage)
Nguyen Phuong Hung
Nguyen Phuong Hung am 21 Okt. 2020
Beantwortet: drummer am 21 Okt. 2020
I am a beginer with matlab and python, so I have a function from matlab like this:
--------
for j=1:n
for i=1:m
T(j,i,1)=0;
end
end
---------
And I converted it to Python code:
def T(x, y, z=1):
for j in range(1, n):
return j
for i in range(1, m):
return i
return T(i ,j ,1)
I'm still not sure about the answer above. Are there ideas to complete this problem?
  1 Kommentar
Stephen23
Stephen23 am 21 Okt. 2020
Your MATLAB code is just
T = zeros(n,m);
You can do the same with numpy.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

drummer
drummer am 21 Okt. 2020
What is your question, actually? What do you want to complete?
Your MATLAB code generated a 2D matrix of zeros, and it is as simple as Stephen showed you. You can use for loops, but using zeros is rather easy.
Also, you don't need the third dimension if it is 1. Just go for T(j, i) in your code.
For python, you could instead:
import numpy as np
matrixDim = (2,2) # Or declare n and m as you wish.
np.zeros(matrixDim)
Check if you get this output.
array([[ 0., 0.],
[ 0., 0.]])
Cheers

Kategorien

Mehr zu Call Python from MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by