Save sub-matrix column and row in another matrix

I have a 2D array with 4 column and many rows.
Month Day Year Value
xx xx xxxx xxx.xxxx
I want to enter the year from keyboard then save in an other matrix all the rows (month, day,year,value info) that contain the year from the input I entered. I've tried this
for idx = 1:numel(array)
element = array(idx);
if(element == month)
..... "Don't know how to do..."
end
end
thank you in advance

 채택된 답변

KL
KL 2017년 10월 22일
편집: KL 2017년 10월 23일

1 개 추천

your_result = your_array(your_array(:,3)==your_choice_year,:);
but an efficient way to store such data is to use a table.
To create a table,
T = array2table(your_array, 'v',{'month','day','year','val'})
to extract year,
res = T(T.year==your_choice,:)
same goes for month or day

댓글 수: 1

I'm working with tables now. Is it possible to extract month and year at the same time?
Something like
res = T(T.year==1995,:) & T(T.month==1,:)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2017년 10월 22일
편집: Andrei Bobrov 2017년 10월 22일

1 개 추천

Let a - your array [Month Day Year Value]
out = a(a(:,3) == input_year,:);

댓글 수: 1

giasco
giasco 2017년 10월 23일
It works great. And if I want to extract month and year ?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2017년 10월 22일

댓글:

2017년 10월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by