Finding a specific row in a matrix based on user inputs?

8 ビュー (過去 30 日間)
Nicholas DeGarmo
Nicholas DeGarmo 2020 年 2 月 26 日
コメント済み: KALYAN ACHARJYA 2020 年 2 月 27 日
I have a matrix with 365 rows and 7 columns, the first column is the month (1-12) and the second column is a day of that month. The user inputs what month they want to look at in a numeric value (1-12) and then the day they want (1-31), but then I need to take that input data and only find the array with those two specific values, I have no clue how to copy just that row based on those two inputs.
Images to explain:

回答 (2 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 2 月 27 日
編集済み: KALYAN ACHARJYA 2020 年 2 月 27 日
#Do the necessary changes, as required
%let say mat_data is the main matrix
mat_data=[1 1 3 4 56
1 2 3 4 50
1 3 4 5 90
2 3 5 6 70];
month=input('Enter the Month Any value 1-12: ');
day=input('Enter the Day Any value 1-31: ');
disp('The details is ');
mat_data(mat_data(:,1)==month & mat_data(:,2)==day,:)
  2 件のコメント
KSSV
KSSV 2020 年 2 月 27 日
Why to use find when we have logical indexing at best?
KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 2 月 27 日
Yes, thanks for pointing it out

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


KSSV
KSSV 2020 年 2 月 27 日
編集済み: KSSV 2020 年 2 月 27 日
Let A be your matrix of size 365*7.
themonths = A(:,1) ;
thedays = A(:,2) ;
% Let us say I want 7th month and 7th day indices
m = 7 ; d = 7 ;
idx = themonths == m && thedays == d ;
iwant = A(idx,:) ;

カテゴリ

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

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by