Need to access only few samples from and an array
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi I have a matrix and instead of traversing sequentially I need to traverse randomly. So I use another array to random the index of the matrix and use if for traversing. Now From that pseudorandom i do not want one row of the matrix to be processed and I am not getting desired result. Can you help me with it. Below is the code and explanation of the same.
a=[10 12 16 15; 3 4 8 0; 14 11 4 1; 7 6 15 9;]
b=a';
d=b(:)';
[r,c]=size(a);
seed_key = 123456; % key
rng(seed_key); % seed the random number generator
idxs1 = randperm(r*c);
cnt_rnj=1;
half=r*c/2;
halfend=half+r;
inx=find(idxs1>half & idxs1<halfend+1);
for i=1:length(d)
if (i~=idxs1(inx(cnt_rnj)))
% idd(i)=1; //to check how the loop is processing
else
%idd(i)=2; // to check how the loop is processing
cnt_rnj=cnt_rnj+1
YN=i
end
end
Basically if it is a 4x4 matrix I do not wanted index position 9,10,11,12 to be processed. If I had to do it sequentially it is not an issue but as I am traversing the randomly the index position will not be in order in the idxs1 matrix. Here for example matrix inx has the position of [9,10,11,12] of array d as [1,7,9,11]. Using the if (i~=idxs1(inx(cnt_rnj))) it evaluates position 9,11,12 but misses 10th index. Can you please help me to solve this.
0 commentaires
Réponse acceptée
dpb
le 11 Mar 2018
"...I do not wanted [sic] index position 9,10,11,12 to be processed."
If you mean original indices then just remove them from the index vector before processing the remainder--
ixExclude=[9:12]; % the exclusion set
idxs1 = randperm(r*c);
idx=setdiff(idxs1,ixExclude,'stable'); % the permuted set excluding unwanted
for i=1:length(idx)
...
do whatever with those wanted here
...
More than likely you can eliminate the for...end loop over the indices and use the selection index vector in Matlab array syntax operations as well.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!