Compare column vector to column in a Matrix and extract data
2 次查看(过去 30 天)
显示 更早的评论
I have a column vector A=1x5 (timestamps) and a Matrix B=20x5 (time + pressure readings). I want to look at B(:,1) and find where the values in A match up- then extract the data up to this point. For example
A = [5; 10; 15; 20; 25] B(:,1) = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20]
value of A(1,1) = value of B(5,1) so I want to make 3rd matrix: D(:,1) = [1; 2; 3; 4; 5] (+ pressure readings in other columns) then the value of A(2,1) = value of B(10,1) so I want 4th matrix E(:,1) = [6; 7; 8; 9; 10]
So far I know you will need two for loops... and an if/while statement maybe? Also not too sure how to store the data each time etc.
Any help is much appreciated, thanks!
0 个评论
回答(2 个)
Stephen23
2018-11-7
编辑:Stephen23
2018-11-7
Do not use loops for this. MATLAB is a high-level langauge, it is not C++.
It is not completely clear what you expect to get, but here is one approach that groups the data into a cell array, using histc and accumarray:
>> A = [5;10;15;20;25];
>> B = [1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20];
>> [~,idx] = histc(B,0.5+[0;A]);
>> C = accumarray(idx,B(:,1),[],@(v){v});
>> C{:}
ans =
1
2
3
4
5
ans =
6
7
8
9
10
ans =
11
12
13
14
15
ans =
16
17
18
19
20
From your description, it seems that you should probably be using a table to store your data, which would make it easy to group data and apply functions to the groups.
1 个评论
Silver
2018-11-13
Hello Stephen !
I have a similar problem , Actually I have two kind of data :
- netcdf file (contain latitude , longitude , time , and chlorphyle as main parameter)
- xlsx file
The main parameter data (Chl) is having this format 818x839x120 double : 3D
The xlsx data form a 2D line like the photo below (longitude, latitude and chlorophyle as main parameter) :
![Comparison_CHL_bloom227_2017.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/194797/Comparison_CHL_bloom227_2017.png)
Actually I want to extract chlorophyle data from the netcdf file while its latitude and longitude of correspond of those from xlsx file . So the idea is to extract the range of the coincidance points between long and lat from both files into a vector.
I used this code that does not work as I wish so :
B=[]; %vector of range of similitude points
% latitudebis : latitude from ferrybox data file
% longitudebis : longitude from ferrybox data file
% m : latitude from satellite data file
% n : longitude from satellite data file
for b=1:818 % 818 is the size of the latitude and longitude
if ((m(1,b)== latitudebis(1,b)) && (n(1,b)== longitudebis(1,b))) == 0
B (1,b) = b
end
end
I will appreciate your help again !
KSSV
2018-11-15
ncfile = youfilename ;
lon = ncread(ncfile,'lon') ; nx = length(lon) ;
lat = ncread(ncfile,'lat') ; ny = length(lat) ;
t = ncread(ncfile,'time') ; nt = length(t) ;
[X,Y] = meshgrid(lon,lat) ;
% Let loni,lati be your locations from the excel for which you want the extract chlorophyll data for each time
iwant = cell(nt,1) ;
for i = 1:nt
chl = ncread(ncfile,'chlorophyll',[1,1,i],[nx,ny,1]) ;
% surf(lon,lat,chl') ; shading interp ; colorbar ; drawnow
chli = interp2(X,Y,chl',loni,lati) ;
iwant{i} = chli ;
end
The above code should do what you wanted. I have assumed the netCDF variables as lon,lat, time and chlorophyll. If they vary in your file, do change them.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NetCDF 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!