How to trim data from a double variable?

8 views (last 30 days)
Lets say we have a double variable 10x10 array, and entries in its 3rd row are (1,2,5,6,5,6,7,8,2,4), we need the values corresponding to data between 4 and 8 for this row, i.e. there are 7 valid entries in this row so we need the final output as a double variable 10x7 only. Thank you in advance!
let me clarify it with example: fullArray2D =
6 2 1 5 2 6 8 3 2 4
6 8 5 7 6 6 8 5 6 5
7 5 2 5 6 1 5 6 3 7
9 8 2 5 4 9 2 1 9 8
9 2 2 5 2 7 4 8 2 1
7 5 2 2 9 7 2 6 9 2
6 6 2 5 1 1 1 8 5 4
9 1 1 8 1 8 9 4 7 1
6 6 6 8 2 9 3 5 9 5
1 4 3 3 2 9 3 1 3 4
i want an output as following:
output=
6 2 5 2 8 3 4
6 8 7 6 8 5 5
*7 5 5 6 5 6 7*
9 8 5 4 2 1 8
9 2 5 2 4 8 1
7 5 2 9 2 6 2
6 6 5 1 1 8 4
9 1 8 1 9 4 1
6 6 8 2 3 5 5
1 4 3 2 3 1 4
hope this clarifies it.
  3 Comments
Umesh
Umesh on 31 Mar 2013
sorry there was a glitch in there, i wanted an output of dimension 10x7. I want to operate on row three and extend it for corresponding column.
Image Analyst
Image Analyst on 31 Mar 2013
When you're operating on row 3 (a row vector of 10 elements), what does "the corresponding column" mean (corresponding to what?), and what does "extend it" mean?

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 31 Mar 2013
Edited: Image Analyst on 31 Mar 2013
Try this:
% Extract row #3 only from the full 2D array:
row3 = = fullArray2D(3, :);
% Find columns meeting our criteria:
columnsMatchingCriteria = row3>4 & row3<8; % A logical array
% Now extract those elements meeting the criteria:
extractedElements = row3(columnsMatchingCriteria);
If there are different numbers and you need it for each row, then:
fullArray2D = randi(9, 10,10)
for row = 1 : size(fullArray2D, 1)
% Extract row #3 only from the full 2D array:
thisRow = fullArray2D(row, :);
% Find columns meeting our criteria:
columnsMatchingCriteria = thisRow>4 & thisRow<8; % A logical array
% Now extract those elements meeting the criteria:
validNumbers = thisRow(columnsMatchingCriteria);
% Print out to command window.
fprintf('There are %d valid numbers for row #%d are: ', length(validNumbers), row);
fprintf('%d ', validNumbers);
fprintf('\n');
% Store in cell array. Need cell array because length(validNumbers)
% might tbe different for different rows.
extractedElements{row} = validNumbers;
end
In the command window:
fullArray2D =
5 6 4 9 3 3 5 1 9 8
5 7 2 4 1 5 6 6 5 8
8 4 3 1 2 9 3 3 7 4
4 6 3 4 7 7 2 9 6 6
5 8 3 7 7 8 5 9 1 8
9 1 2 8 8 4 4 3 5 9
1 1 4 5 6 5 8 8 6 7
9 9 2 7 1 6 8 9 5 2
2 6 8 9 9 3 7 6 4 6
7 3 1 1 8 7 2 8 9 1
There are 3 valid numbers for row #1 are: 5 6 5
There are 6 valid numbers for row #2 are: 5 7 5 6 6 5
There are 1 valid numbers for row #3 are: 7
There are 5 valid numbers for row #4 are: 6 7 7 6 6
There are 4 valid numbers for row #5 are: 5 7 7 5
There are 1 valid numbers for row #6 are: 5
There are 5 valid numbers for row #7 are: 5 6 5 6 7
There are 3 valid numbers for row #8 are: 7 6 5
There are 4 valid numbers for row #9 are: 6 7 6 6
There are 2 valid numbers for row #10 are: 7 7
  9 Comments
Umesh
Umesh on 31 Mar 2013
Yeah! It works. thank you!!
Image Analyst
Image Analyst on 31 Mar 2013
Mark the answer as "Accepted" then. Thanks.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!