Effacer les filtres
Effacer les filtres

Fill a matrix from a matrix of indices

10 vues (au cours des 30 derniers jours)
Farshid
Farshid le 31 Août 2023
Commenté : Voss le 31 Août 2023
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 commentaires
Voss
Voss le 31 Août 2023
"Please find the attached files for your reference."
I don't find any files attached. Can you double-check?
Farshid
Farshid le 31 Août 2023
Sorry, Please find the attached files here, Thanks

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 31 Août 2023
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 commentaires
Farshid
Farshid le 31 Août 2023
Dear Voss, Thanks, it was very helpful command, I just change NaN zeros, as below;
result = zeros([n_months size(mask)]);
Cheers,
Voss
Voss le 31 Août 2023
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by