Save sub-matrix column and row in another matrix
4 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
KL
on 22 Oct 2017
Edited: KL
on 23 Oct 2017
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
More Answers (1)
Andrei Bobrov
on 22 Oct 2017
Edited: Andrei Bobrov
on 22 Oct 2017
Let a - your array [Month Day Year Value]
out = a(a(:,3) == input_year,:);
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!