How to get the values in the array and use it for user input, then creating an array for the inputs?

6 ビュー (過去 30 日間)
I have an array=(5 6 8) how can i ask an input for f(5),f(6),f(8) and put all inputs in an array

採用された回答

Parag Jhunjhunwala
Parag Jhunjhunwala 2023 年 6 月 26 日
編集済み: Parag Jhunjhunwala 2023 年 6 月 26 日
The below code asks the user to enter the values for f(5), f(6) and f(8) and inserts the user input in the vector f at the respective positions(i.e. 5,6, and 8):
arr=[5 6 8];
for i=1:length(arr)
prompt = ['f(' num2str(arr(i)) '):'];
f(arr(i)) = input(prompt);
end
  3 件のコメント
Parag Jhunjhunwala
Parag Jhunjhunwala 2023 年 6 月 27 日
MATLAB only supports positive indices. So, I think it's not possible.
Stephen23
Stephen23 2023 年 6 月 27 日
編集済み: Stephen23 2023 年 6 月 27 日
"but it seems like it doesn't support a negative index any way to make it possible? "
You are confusing data with code. Better to keep them separate and iterate over indices, not over (meta-)data:
X = [5,6,8];
Y = nan(size(X));
for k = 1:numel(X)
Y(k) = input("f("+k+")");
end
More robust would be to use INPUT's 's' option and STR2DOUBLE.

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

その他の回答 (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