필터 지우기
필터 지우기

Select a random cell from multiple matrices

조회 수: 1 (최근 30일)
Geant Bepi
Geant Bepi 2015년 5월 14일
댓글: Geant Bepi 2015년 5월 14일
let's say 3 matrices,
A = 30x20; B = 30x20; C = 30x20;
All three matrices contain NaNs.
I want to select a random value from each row out of ALL three matrices but none of the corresponding values should be a NaN. For e.g., if the random value is A(1,10) then B(1,10) and C(1,10) should not contain a NaN. Also it has to be the same cell number(row,column) for all matrices. If nothing matches moves to the next row.
And then repeat it for each row until the end.
  댓글 수: 2
Purushottama Rao
Purushottama Rao 2015년 5월 14일
What is there inside A B C? does they contain Nan in few positions? What do you expect as output? is it a 30*20 matrix containing 1 s and 0 s?
Geant Bepi
Geant Bepi 2015년 5월 14일
A, B and C matrices contain just random numbers (integers). Nans are just all over.
I expect the random value as the output.

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

답변 (1개)

Guillaume
Guillaume 2015년 5월 14일
If you want to go randomly through all the elements of the matrices for which none of the matrices contain a nan, simply use randperm to reorder the linear indices that do not contain NaN in any of your matrices:
validindices = find(~(isnan(A) | isnan(B) | isnan(C))); %linear indices that have no nans in any of the matrices
randindices = validindices(randperm(numel(validindices))) %reorder randomly
If you want subscripts instead of linear indices, just use ind2sub to convert, although you can use the linear indices just fine:
[randrows, randcols] = ind2sub(size(A), randindices);

카테고리

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