To get the row and column of element in a matrix.
11 views (last 30 days)
Show older comments
fyza affandi
on 26 Nov 2018
Commented: Walter Roberson
on 26 Nov 2018
I have 3D matrices.
t(:,:,1) =
1 10 19 28 37 46 55 64 73 82
2 11 20 29 38 47 56 65 74 83
3 12 21 30 39 48 57 66 75 84
4 13 22 31 40 49 58 67 76 85
5 14 23 32 41 50 59 68 77 86
6 15 24 33 42 51 60 69 78 87
7 16 25 34 43 1 61 70 79 88
8 17 26 35 44 53 1 1 80 89
9 18 27 36 45 54 63 72 1 1
t(:,:,2) =
91 100 109 118 127 136 145 154 163 172
92 101 110 119 128 137 146 155 164 173
93 102 111 120 129 138 147 156 165 174
94 103 112 121 130 139 148 157 166 175
95 104 113 122 131 140 149 158 167 176
96 105 114 123 132 141 150 159 168 177
97 106 115 124 133 142 151 160 169 178
98 107 116 125 134 143 152 161 170 179
99 108 117 126 135 144 153 162 171 180
How to know the row and column of 43 in t(:,:,1)?
The result that will come out will like this
a= (7,5,1)
0 Comments
Accepted Answer
Walter Roberson
on 26 Nov 2018
idx = find(t == 43);
[r, c, p] = ind2sub(size(t), idx);
a = [r, c, p];
More Answers (1)
madhan ravi
on 26 Nov 2018
Edited: madhan ravi
on 26 Nov 2018
[idx,idy]=find(ismember(t(:,:,1),43));
result=[idx idy]
8 Comments
Walter Roberson
on 26 Nov 2018
t1 = [202 69 224 2 187 248 40 102 184 206;128 11 101 153 42 47 129 148 78 202;142 76 117 169 72 238 145 5 118 73;161 142 54 148 67 12 49 148 13 18;25 248 194 233 140 62 83 238 99 15;63 176 140 163 139 3 183 28 93 163;158 184 92 135 201 172 141 187 74 109;78 143 179 67 222 231 37 248 209 231;196 137 28 14 201 146 97 156 115 107];
t2 = [40 18 61 45 56 130 254 39 140 220;138 47 8 30 223 159 84 35 54 115;239 147 168 45 55 188 35 181 201 167;169 48 41 160 214 59 99 119 135 78;101 75 205 215 220 6 144 29 146 155;67 118 105 131 134 36 162 179 108 72;217 89 84 43 122 197 139 46 184 204;241 82 191 183 227 248 81 205 19 204;97 118 191 232 17 99 41 132 152 244];
t = cat(3, t1, t2);
a= [43 50 67];
[idx, idy] = find(t==a(1));
result = [idx idy];
>> result
result =
7 14
Now, what does that mean? Row 7, column 14? But there are only 10 columns in t.
In this example, the 43 is located at Row 7, Column 4, Pane 2.
>> idx = find(t == 43);
[r, c, p] = ind2sub(size(t), idx);
a = [r, c, p];
>> a
a =
7 4 2
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!