What is ind2sub

23 ビュー (過去 30 日間)
Varun Nair
Varun Nair 2021 年 7 月 19 日
編集済み: Walter Roberson 2021 年 7 月 23 日
This is the code where I had doubt on the syntax of ind2sub
%Parameters
A=0.01; %Wb is the unit of A
B=0.001; %H is the unit of Inductance
I =(0:1:2); %row!
theta=(0:1:45).'; %column!
T = - A .* I .* cosd(theta) + B .* I.^2 .* sind(2*theta);
surf(I, theta, T)
xlabel('I'); ylabel('theta'); zlabel('Torque')
[maxT, idx] = max(T(:));
[r,c] = ind2sub(size(T), idx);
best_theta = theta(r);
best_I = I(c);
fprintf('best torque is %g at theta = %g and I = %g\n', maxT, best_theta, best_I);

採用された回答

Star Strider
Star Strider 2021 年 7 月 20 日
編集済み: Walter Roberson 2021 年 7 月 23 日
MATLAB uses linear indexing for arrays, however many arrays are also referred to by subscripts into the rows, columns, and additional dimensions. The ind2sub function converts the linear indices into subscripts.
In this example:
[maxT, idx] = max(T(:));
[r,c] = ind2sub(size(T), idx);
the ind2sub call takes linear index ‘idx’ and converts it to the appropriate subscripts into matrix ‘T’. (It appears to be completely appropriate here.)
See the documentation section on Matrix Indexing for an extended discussion.
.
  2 件のコメント
Varun Nair
Varun Nair 2021 年 7 月 20 日
Thanks for the answer @Star Strider: I am not able to visualise the effect of idx in this.
Star Strider
Star Strider 2021 年 7 月 20 日
As always, my pleasure!
.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by