save values of 2D matrix into a multidimensional matrix
Afficher commentaires plus anciens
Hello,
I have a for loop where a I generates two new points in every loop for my 2xn matrix where n=15001.
I want to save values from this matrix that meets 4 conditions into m different 2 x k matrixes (sizes are not the same for each matrix) where m=221. Maybe I need to use cells but I'm having issues when creating the nested for loop for this operation, I can not achieve what I'm aiming for, I need help please. Here my code:
for ind2=2:15001
%some previous code that generates points for x1x2 matrix (its long)
% save values of x1d x2d
x1x2(:,ind2)=[x1d(1,ind2);x2d(ind2)];
for indX1=1:16
for indX2=1:12
if (x1x2(1,ind2)>X1(indX1) && x1x2(1,ind2)<X1(indX1+1) && x1x2(2,ind2)>X2(indX2) && x1x2(2,ind2)<X2(indX2+1))
%Here my issue, Im not able to create this
%multidiemnsional matrix
ROIx1x2(:,count,m)= x1x2(:,ind2);
F(count,1,m)= fintd(:,ind2);
count=count +1;
end
end
end
end
7 commentaires
Dyuman Joshi
le 26 Sep 2022
What do you mean by 'I m not able to create this multidimensional matrix'? Does the matrix not exist? Or does it not contain the expected output?
If sizes are not equal for each iteration/matrix, you do need to use cell array.
Also, you are running 15000*16*12 iterations which is 2.88 Million iterations. It's going to be slow unless you have a powerful CPU.
Jan
le 26 Sep 2022
Is x1x2, ROIx1x2 and F pre-allocated? The iterative growing of arrays is extremely expensive.
Benjamin Thompson
le 26 Sep 2022
Please post a working small size example of your problem so we can better understand. I see reference to an unknown function findt.
Mikel
le 27 Sep 2022
@Mikel: You can pre-allocate arrays with a maximum size and crop them afterwards:
x = zeros(1e6, 1);
ix = 0;
for k = 1:1e6
if rand > 0.5
ix = ix + 1;
x(ix) = 27;
end
end
x = x(1:ix);
This reserves 8 MB. Without an preallocation, a new array is created in each iteration with matching condition, so you allocate about sum(1:0.5e6)*8 Byte, which is 1 TB. Although most of this memory is released soon, allocating it is expensive.
You can accelerate the code:
for ind2=2:15001
for indX1=1:16
for indX2=1:12
if (x1x2(1,ind2)>X1(indX1) && x1x2(1,ind2)<X1(indX1+1) && x1x2(2,ind2)>X2(indX2) && x1x2(2,ind2)<X2(indX2+1))
% ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
If this condition is FALSE, running the inner loop over indX2 is a waste of time. Better:
for ind2=2:15001
for indX1=1:16
if x1x2(1,ind2) > X1(indX1) && x1x2(1,ind2) < X1(indX1+1)
for indX2=1:12
if x1x2(2,ind2)>X2(indX2) && x1x2(2,ind2)<X2(indX2+1)
Mikel
le 28 Sep 2022
J. Alex Lee
le 28 Sep 2022
Modifié(e) : J. Alex Lee
le 28 Sep 2022
The value of m is static, is it supposed to be something like sub2ind(size(PDLUT),indX1,indX2) or something?
I wonder if what you are looking for ultimately is something like a 2D histogram, or you can extract what you need from it:
histogram2(x1x2(1,:),x1x2(2,:),X1,X2)
[N,XEDGES,YEDGES,BINX,BINY] = histcounts2(x1x2(1,:),x1x2(2,:),X1,X2);
Réponses (0)
Catégories
En savoir plus sur MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!