Fill a matrix from a matrix of indices

조회 수: 3 (최근 30일)
Farshid
Farshid 2023년 8월 31일
댓글: Voss 2023년 8월 31일
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
Voss 2023년 8월 31일
"Please find the attached files for your reference."
I don't find any files attached. Can you double-check?
Farshid
Farshid 2023년 8월 31일
Sorry, Please find the attached files here, Thanks

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

채택된 답변

Voss
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)))
1.0e+03 * 6.3734 6.5652 6.9272 7.2473 7.5251 7.6318 7.5505 7.3966 7.2202 7.0659 6.9370 6.7665
disp(my_flow(:,1))
1.0e+03 * 6.3734 6.5652 6.9272 7.2473 7.5251 7.6318 7.5505 7.3966 7.2202 7.0659 6.9370 6.7665
isequal(result(:,J2(1),I2(1)),my_flow(:,1))
ans = logical
1
  댓글 수: 2
Farshid
Farshid 2023년 8월 31일
Dear Voss, Thanks, it was very helpful command, I just change NaN zeros, as below;
result = zeros([n_months size(mask)]);
Cheers,
Voss
Voss 2023년 8월 31일
You're welcome!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by