Filter löschen
Filter löschen

Storing a row of data into a cell array if it satisfies a condition, if not then disregard and move onto the next row

2 Ansichten (letzte 30 Tage)
I am trying to sort data into cell arrays according to whether or not they satisfy the if statement. CellPosition is a 10x3 double and CurrentCellPosition is a 1753x3 double (x, y, z coordinates). Basically I want to loop over i (the number of cell positions) and find the difference between the first row of CellPosition and every value in CurrentCellPosition, then I want it to select the CurrentCellPosition(s) which lie within the ranges in the if statement and place them in the first element of a cell array. I then want it to do the same thing for each i (from 1 to 10) leaving me a 1x10 cell array. My code below runs alright but when it comes to writing the values to a cell array I can't seem to figure out where I am going wrong. Right now it is just writing all 1753 elements to each of the 10 elements in the cell array.
CellRadius = 1 * 10^4 %nm
NucleusRadius = 7 * 10^3 %nm
NumberofCells = 10;
CellPosition = csvread('cell.csv');
for i = 1:NumberofCells
for j = 1:length(Damage)
CurrentCellPosition = cell2mat(Damage(:,2:4));
X = abs(CurrentCellPosition(:,1) - CellPosition(i,1)) * 10^3; %Convert to nm
Y = abs(CurrentCellPosition(:,2) - CellPosition(i,2)) * 10^3;
Z = abs(CurrentCellPosition(:,3) - CellPosition(i,3)) * 10^3;
if (CellPosition(i,1) <= X < CellRadius) | (CellPosition(i,1) <= X < NucleusRadius) &...
(CellPosition(i,2) <= Y < CellRadius) | (CellPosition(i,2) <= Y < NucleusRadius) & ...
(CellPosition(i,3) <= Z < CellRadius) | (CellPosition(i,3) <= Z < NucleusRadius);
Ionisations{i}(j,:) = Damage(j,:)
else
continue
end
end
end
  2 Kommentare
dpb
dpb am 14 Mär. 2020
Attach .mat file w/ small(ish) set of the data to observe/test...w/ desired results.
mel1708
mel1708 am 14 Mär. 2020
I cut Damage down from 1753x3 to 201x3. I also took out the data that isn't needed for this part so the first line in the double for loop just has to be
CurrentCellPosition = cell2mat(Damage(:,:));

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 15 Mär. 2020
You don't need the inner for loop. Following code shows how you can do it using vectorized operations
CellRadius = 1 * 10^4; %nm
NucleusRadius = 7 * 10^3; %nm
NumberofCells = size(CellPosition,1);
CurrentCellPosition = cell2mat(Damage);
for i = 1:NumberofCells
CellPosition_row = CellPosition(i, :);
error = (CurrentCellPosition - CellPosition_row)*1000;
X_err = error(:,1);
Y_err = error(:,2);
Z_err = error(:,3);
mask = (X_err < CellRadius | X_err < NucleusRadius) & ...
(Y_err < CellRadius | Y_err < NucleusRadius) & ...
(Z_err < CellRadius | Z_err < NucleusRadius); % correct the condition if it is wrong
Ionisations{i} = CurrentCellPosition(mask, :);
end
Check if the condition in the above code is correct.
Also, note that in MATLAB, the expression a < b < c might not be what you think it is. For example, try
-5 < -3 < -1

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by