Compare each elements in a column to a set number, and output results

Hi all,
I am doing a migration project and having a 289x50 matrix. Each column is the time-lapse displacement. I am trying to set up a value to sort out the elements that moves enough. For example, if a value in a certain place of that column is over 100, mark this column number, stop afterwards search within the same column, and move to the next column. This process continues until the last column is been exexcuted.
I think I can creat a same size matrix to do a matrix compare, but it seems very redundant. I wonder there will be a better and efficient way to make this happen, but having a hard time to find correct info. I really appreciate if someone can provide some hints and directions.

 Accepted Answer

Try this:
M = randi(200, 289, 50)
M = 289×50
24 154 168 107 159 139 160 192 111 61 14 125 177 107 175 24 68 129 192 99 172 94 200 2 200 172 153 135 103 159 16 58 105 171 133 137 87 9 172 113 130 88 28 22 177 110 68 3 160 114 21 6 169 142 105 189 170 96 190 5 138 200 184 154 73 46 127 63 165 125 87 200 28 17 200 158 168 82 143 179 191 180 73 162 165 110 53 158 70 109 66 144 97 94 138 40 112 111 99 117 7 54 80 19 176 46 81 130 13 91 98 54 23 85 42 102 86 111 191 77 104 76 23 178 99 55 14 55 81 85 164 23 47 78 54 60 81 171 81 8 17 36 103 166 117 104 152 62 90 17 170 105 21 100 165 80 191 176 19 66 183 49 159 93 163 163 195 144 7 83 90 144 73 33 72 19 183 19 22 183 122 3 4 110 155 169 91 19 157 119 41 164 26 95 138 15 134 37 163 175 88 52 2 90 101 116 8 38 83 174 123 66 43 61 32 172 109 31 180 39 151 170 145 174 86 86 12 146 102 103 181 108 131 81 91 128 190 147 35 38 165 169 39 126 126 99 105 104 182 121 38 30 58 172 106 69 23 76 152 70 34 95 168 60 193 61 87 29 3 161 54 116 150 19 42 151 101 165 183 47 119 72 57 183 39 8 79 30 15 23 181 91 49 190 7 97 199 146 24 13
[rows, columns] = find(M > 100)
rows = 7232×1
3 5 6 7 8 9 16 17 18 21
columns = 7232×1
1 1 1 1 1 1 1 1 1 1
Each corresponding pair of rows and columns will be a row and column where the matrix M is more than 100.

5 Comments

Hi, thank you. That's a good idea. But I just realized I haven't made it clear in the main part. My goal is to just see if there is a elements in each column larger than set value. If there is a value over set value, then stops all afterwards searching within the same column and moves to the next column till the last column.
M = randi(200, 289, 50)
M = 289×50
120 94 168 194 57 17 62 91 46 62 58 76 114 75 18 3 15 91 184 65 195 114 176 95 12 186 52 124 45 154 104 152 131 107 147 37 58 39 37 176 167 114 46 90 157 177 71 166 27 39 111 15 99 110 132 15 73 84 1 47 119 16 198 192 61 75 69 173 190 159 154 26 139 110 45 185 145 11 32 68 168 98 7 115 13 177 82 95 16 118 110 189 197 32 89 33 173 132 186 137 94 160 90 68 125 200 90 16 172 179 96 88 76 33 55 147 176 86 90 6 169 178 173 53 104 143 150 13 135 84 93 172 104 76 173 108 174 16 27 70 151 40 66 103 101 12 185 153 22 181 116 86 171 114 99 49 121 111 95 125 195 11 72 12 69 39 175 2 147 88 100 192 75 190 62 116 149 1 9 194 196 27 30 92 40 94 9 169 22 113 189 107 17 169 12 177 104 162 125 50 114 46 78 104 1 132 185 119 21 198 173 93 169 180 136 5 62 61 127 18 190 19 30 18 86 194 188 46 178 34 195 4 188 159 38 112 193 90 107 115 136 126 177 114 176 187 182 75 78 67 140 144 85 154 125 71 74 194 38 149 189 107 82 57 195 187 47 52 46 170 124 187 90 162 26 102 56 200 21 150 75 178 117 179 35 148 127 75 109 76 134 170 11 50 157 138 114 180 20 1
[rows, columns] = size(M);
firstRow = nan(1, columns);
for col = 1 : columns
thisColumn = M(:, col);
% Find first row where this column of M exceeds 100.
row = find(thisColumn > 100, 1, 'first');
if ~isempty(row)
% Found one!
firstRow(col) = row;
end
end
% Show in command window.
firstRow
firstRow = 1×50
1 2 1 1 2 5 4 3 3 2 2 2 1 3 2 2 3 2 1 4 1 1 1 2 2 1 4 1 8 1
This works! I integrated this to another code I am using and found there are NaN outputs. Is there a way to export column number exceeding set value without disturbing the original order?
The reason there are nan's there is that there is no element in the column that is more than 100. If you remove them, then you will no longer have a 50 element wide vector. It will be less, like say 40 long, and those 40 would not correspond to the original 50 column matrix, which could be very confusing. I suggest you leave it and learn how to handle that case, so that the value of firstRow corresponds with the column of the original matrix. If you really want to delete them, you can use isnan
firstRow(isnan(firstRow)) = []; % Dangerous/risky.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 10 Apr 2022

Commented:

on 10 Apr 2022

Community Treasure Hunt

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

Start Hunting!