Extracting a value from a 3D table

1 回表示 (過去 30 日間)
Jerry Smith
Jerry Smith 2019 年 11 月 23 日
回答済み: Les Beckham 2019 年 11 月 23 日
So Inf or Nan were in the first row, column ... then the code would output (1,1,1).

採用された回答

Stijn Haenen
Stijn Haenen 2019 年 11 月 23 日
Maybe this works, for output(a,b,c):
Num_nan=find(isnan(Table));
c=floor(Num_nan/(81*81))+1;
b=floor(rem(Num_nan,81*81)/81)+1;
a=rem(rem(Num_nan,81*81),81);
  1 件のコメント
Jerry Smith
Jerry Smith 2019 年 11 月 23 日
This works thank you but I dont really understand the code, is there a chance you could explain it?

サインインしてコメントする。

その他の回答 (2 件)

Stijn Haenen
Stijn Haenen 2019 年 11 月 23 日
Sure,
In the first line "Num_nan=find(isnan(Table));" you get the positions of the NaN, but this is just a number. For example, applying this on the following matrix:
[1 2 3;
4 NaN 6;
7 8 9]
Num_nan = 5
But then you should use this number to get the row and column and this is done with the other lines.
There are 3 numbers in one column, so
floor(Num_nan/3)+1=2;
So the column number is 2,
for the row we use the remaining of 5/3 which is 2
so NaN is at (2,2).
  1 件のコメント
Jerry Smith
Jerry Smith 2019 年 11 月 23 日
thanks so much

サインインしてコメントする。


Les Beckham
Les Beckham 2019 年 11 月 23 日
A 3d example using Matlab's indexing functions instead of floor and rem:
t3=[1 2 NaN;
4 5 6;
7 8 9]; % a 2d matrix with one NaN
t3(:,:,2) = t3'; % add another dimension using the transpose of the first 'page'
idx = find(isnan(t3)); % find the linear indices of the NaNs in the 3d array
[r, c, p] = ind2sub(size(t3), find(isnan(t3))); % convert the linear indices to array indices
% print out the results
fprintf('r\t\c\tp\n');
for (i = 1:length(r))
fprintf('%d\t%d\t%d\n', r(i), c(i), p(i));
end
The find() function returns the linear indices (see Array Indexing) of the NaNs. The ind2sub() function converts those to row, column, and 'page' indices.
The size() function determines the dimensions of the input array so that you don't have to hard code them.
Note that this approach can be extended to more than three dimensions as well (see the documentation for ind2sub).

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by