MATLAB code partially completes task - no error.

조회 수: 1 (최근 30일)
Nolan Miller
Nolan Miller 2019년 6월 27일
댓글: Nolan Miller 2019년 6월 28일
Hello all,
I have a table that consists of about 30,000 to 40,000 rows of data (depending on input file), once the table is produced, I have a segment of code that combs through three specific columns marking each position where the value is less than 2. The second segment then goes through this newly created matrix, removing any duplicates, and then removes the rows with any value less than 2 in the three columns from the original table. Each time I run these two segments, the size of my table will decrease by about 2 or 3, but then the code stops running without showing any error message. I don't know if there is a problem with my code or if I am processing too much at once. Thank you for your assistance! (MATLAB Version: 9.3.0.713579 (R2017b))
%First Segment
vox_min = 2; %threshold value
removesmall = []; %Initial array for collecting positions of values less than vox_min
for k = 1:size(pore_char.BoundingBox, 1)
for j = [4, 5, 6]
if pore_char.BoundingBox(k,j) < vox_min
removesmall = [removesmall, k]; %collects all positions less than vox_min
end
end
end
%Second Segment
removesmall_unique = unique(removesmall); %removes duplicate rows to ensure correct rows are deleted
for i = 1:size(removesmall_unique)
pore_char(removesmall_unique(i), :) = []; %removes rows with values less than vox_min
end
  댓글 수: 2
Joel Bay
Joel Bay 2019년 6월 27일
편집: Joel Bay 2019년 6월 27일
Hi,
First tip, you can use length() instead of size(), length() returns the number of elements in the largest dimension.
To be clear, your code hopes to simply remove only 1 of duplicate small values in the 4th 5th and 6th collumns? So if you had 3 duplicate small values you would keep two of them?
You may want to look at logical indexing. Here's code that would remove all small values.
pore_char.BoundingBox = pore_char.BoundingBox((pore_char.BoundingBox(:,4) > vox_min), :)
pore_char.BoundingBox = pore_char.BoundingBox((pore_char.BoundingBox(:,5) > vox_min), :)
pore_char.BoundingBox = pore_char.BoundingBox((pore_char.BoundingBox(:,6) > vox_min), :)
Heres code that stores all small values in a new array and then removes them from original array:
pore_char.BoundingBoxSmall = pore_char.BoundingBox((pore_char.BoundingBox(:,4) < vox_min), :)
pore_char.BoundingBoxSmall = [pore_char.BoundingBoxSmall pore_char.BoundingBox((pore_char.BoundingBox(:,5) < vox_min), :)]
pore_char.BoundingBoxSmall = [pore_char.BoundingBoxSmall pore_char.BoundingBox((pore_char.BoundingBox(:,6) < vox_min), :)]
pore_char.BoundingBox = pore_char.BoundingBox((pore_char.BoundingBox(:,4) > vox_min), :)
pore_char.BoundingBox = pore_char.BoundingBox((pore_char.BoundingBox(:,5) > vox_min), :)
pore_char.BoundingBox = pore_char.BoundingBox((pore_char.BoundingBox(:,6) > vox_min), :)
Or a more simple example:
A = 10.*rand(100,3); %creates a 100x3 matrix of random numbers between 0 and 10
A = A((A(:,1)>4),:); %redefines A as every row that meets the criteria of the first collumn in that row being > 4.
Nolan Miller
Nolan Miller 2019년 6월 28일
The last example worked! I needed the script to rake through all three of the columns removing any row where any of the three values was less than a value. I planned on using a for loop to repeat the simple example for each column, but the index always went out of range due to the deleted rows.
It's working now with just pasting the same step each time - I'm sure there is a more elegant way to do this, but it works! Thank you very much!

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

채택된 답변

Joel Bay
Joel Bay 2019년 6월 27일
If my comment helps you enough could you accept this answer?

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by