Fill a matrix from a matrix of indices
4 次查看(过去 30 天)
显示 更早的评论
Dear Users,
Please let me to send you the data I am working on it. Please find the attached files for your reference. As you can see in 'my_flow.mat' file, I have 6 river (represented by the each column) for the 12 month (showd in the each row) and with the corresponding indexes of the each one, i.e., 'I2' and 'J2'. You can see the other file is mask file 'mask.mat', that is 2d array. I want to fill 'mask' file with values stored in 'my_flow' file at specific locations using indexes of 'I2' and 'J2'. 'I2' and 'J2' indicates location of each river mouth. In mask file the values of 1 are water (ocean) and 0 (land). mask file is 2d array (y,x), I can reshape it to 3d array (time,y,x) . the
" Adjust the river position relative to the mask "
River positionned in land
--
St Lawrence is J=114 and I=0
" Adjust the river position relative to the mask "
River positionned in land
Saint John is J=27 and I=1
" Adjust the river position relative to the mask "
River positionned in land
--
Petit Mecatina is J=122 and I=86
" Adjust the river position relative to the mask "
River positionned in land
Moisie is J=125 and I=8
" Adjust the river position relative to the mask "
River positionned in land
Natashquan is J=115 and I=62
" Adjust the river position relative to the mask "
River positionned in land
Outardes (R aux is J=141 and I=1
The output I expect should be something of the form below
my_flow(time,y,x),
size of dim;
time = 12;
y = 319;
x = 281;
2 个评论
Voss
2023-8-31
"Please find the attached files for your reference."
I don't find any files attached. Can you double-check?
采纳的回答
Voss
2023-8-31
Something like this?
load my_flow.mat
load mask.mat
load IJ.mat
% visualize mask and I2/J2
p = pcolor(mask);
p.EdgeColor = 'none';
hold on
plot(I2,J2,'or')
% build 3d array result(time,y,x)
[n_months,n_rivers] = size(my_flow);
result = NaN([n_months size(mask)]);
for ii = 1:n_rivers
result(:,J2(ii),I2(ii)) = my_flow(:,ii);
end
% check the result for the 1st river
disp(result(:,J2(1),I2(1)))
disp(my_flow(:,1))
isequal(result(:,J2(1),I2(1)),my_flow(:,1))
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numeric Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!