save values of 2D matrix into a multidimensional matrix

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

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.
Is x1x2, ROIx1x2 and F pre-allocated? The iterative growing of arrays is extremely expensive.
Please post a working small size example of your problem so we can better understand. I see reference to an unknown function findt.
Hello, sorry for not giving all the information you needed. I attached a small example with my real data.
Dyuman Joshi - what I mean is that the matrix that I'm creating is not containing the expected output. I'm getting a 2x15000x221 matrix but, it should be something like, 2x (different sizes) x 221. I understand that somehow matlab is filling this arrays with 0 values but all I want to have is only the values that meets the conditions.
Jan - x1x2 is preallocated but ROIx1x2 and F are not as their sizes change. I know is expensive but I dont know how can I manage this, I was thinking about cells, but not sure.
Benjamin Thompson attached an example code with my real data.
%% INIT
clear variables;
close all;
clc;
%%
%ROIx1x2 and F are not pre-allocated as the size changes.
load('points.mat', 'x1x2','X1','X2','PDLUT','fintd');
m=numel(PDLUT);
count=1;
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))
%Here my issue, Im not able to create this
%multidiemnsional matrix
%my output 2x15000x221
%expected output 2x(different sizes)x221
ROIx1x2(:,count,m)= x1x2(:,ind2);
F(1,count,m)= fintd(:,ind2);
count=count +1;
end
end
end
end
Jan
Jan le 27 Sep 2022
Modifié(e) : Jan 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)
Thank you! this is what I was looking for
J. Alex Lee
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);

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur MATLAB dans Centre d'aide et File Exchange

Produits

Version

R2021b

Question posée :

le 26 Sep 2022

Modifié(e) :

le 28 Sep 2022

Community Treasure Hunt

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

Start Hunting!

Translated by