How to find corresponding value of randomly selected value in a table

1 回表示 (過去 30 日間)
Amethyst McLaughlin
Amethyst McLaughlin 2022 年 5 月 1 日
回答済み: Rahul 2024 年 9 月 11 日
Let's say I have a table of x and y values. I know how to randomly select an x value but need help obtaining the corresponding y value.
  1 件のコメント
Riccardo Scorretti
Riccardo Scorretti 2022 年 5 月 1 日
It depends on the meaning of "corresponding". What is the relationship between x and y? Can you give an example of what you want?

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

回答 (1 件)

Rahul
Rahul 2024 年 9 月 11 日
I understand that you have a table consisting of x and y values. According to your descriptiong, if you have a randomly selected x value from the table. you need to find the corresponding y value from the table.
You can achieve the desired result by using the 'find' function in the following way:
% Considering 'dataTable' as the variable for the table with column names
% as 'X' and 'Y'
randX = 3; % randomly selected x value
% Find the index of the given x value
index = find(dataTable.X == randX);
% Retrieve the corresponding y value
correspondingY = dataTable.Y(index);
fprintf('Corresponding y value: %d\n', correspondingY);
You can refer to the following Mathworks documentation to know more about the 'find' function:
Hope this helps!

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by