フィルターのクリア

How to add to index in for loop?

3 ビュー (過去 30 日間)
Zach Dunagan
Zach Dunagan 2017 年 10 月 20 日
コメント済み: Zach Dunagan 2017 年 10 月 24 日
I am converting Python to Matlab. Here is Python code.
  1. collocation points
xc=np.zeros((numPanels,1))
yc=np.zeros((numPanels,1))
for i in range(numPanels):
xc[i]=(xp[i]+xp[i+1])/2
yc[i]=(yp[i]+yp[i+1])/2
Here is my Matlab code...
% collection of points xc = zeros(numPanels, 1);
yc = zeros(numPanels, 1);
for k = numPanels
xc(k) = (xp(k) + xp(k+1))/2
yc(k) = (yp(k) + yp(k+1))/2
end
xp and yp are arrays. I am trying to add 1 to the second index of xp and yp, where it says xp(k+1) and yp(k+1). Would I need to make another for loop for the indexing that is being added to 1?

採用された回答

KSSV
KSSV 2017 年 10 月 20 日
編集済み: Stephen23 2017 年 10 月 22 日
xc = zeros(numPanels, 1);
yc = zeros(numPanels, 1);
for k = 1:numPanels-1
xc(k) = (xp(k) + xp(k+1))/2
yc(k) = (yp(k) + yp(k+1))/2
end
You are in MATLAB..you can avoid loops here.
xc = (xp(1:end-1)+xp(2:end))/2 ;
yc = (yp(1:end-1)+yp(2:end))/2 ;
But the problem is size of xc and yc will be one less then xp and yp.
  1 件のコメント
Zach Dunagan
Zach Dunagan 2017 年 10 月 24 日
That is fine. Why doesn't this work? I can only see the last value in the array.
% attaching/flipping xp and yp to the 66th % of the zero array
xp(66:end) = flipud(xp(1:numPanels/2));
yp(66:end) = -flipud(yp(1:numPanels/2));
% collocation points
xc = zeros(numPanels, 1);
yc = zeros(numPanels, 1);
for k = numPanels
xc(k) = (xp(k) + xp(k+1))/2;
yc(k) = (yp(k) + yp(k+1))/2;
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by