traversing an array of elements

i've the following list of elements as
block 2=10
block 5=16
block 1= 16
block 7=16
block 4= 23
block 6=31
block 3= 31
i.e i've a total of 7 block numbers
the list is in sorted order...
i want to traverse it and find a match between block values like 'block 5', 'block 1' and 'block 7' match
'block 6' and 'block 3' match.. and print these blocks with matching values
how this can be done?
traversing a single list of array of following type i know like
function traverse
clc;
a=[ 1 2 2 3 3 4 4 4 4]
len=length(a)-1;
for i=1:len
if(a(i)==a(i+1))
fprintf('\n%d and %d\n',a(i),a(i+1));
end
end
but to deal with the above list i dont know help me in doing so....

8 Kommentare

Walter Roberson
Walter Roberson am 13 Mär. 2013
Bearbeitet: Walter Roberson am 13 Mär. 2013
Do you mean that you are starting with strings of that form? If so, what form are the strings stored in?
Raman
Raman am 13 Mär. 2013
Bearbeitet: Raman am 13 Mär. 2013
values are stored in array [s,blockNumber]
i want that when this array is traversed and when there occurs a match it will extract the block like here in this case it will extract block 5 and 1 becoz both have same value and after that continues with traversing and where it founds a match extract that block...
in short i want that where there occurs a match extract those blocks i.e list only those blocks
Walter Roberson
Walter Roberson am 13 Mär. 2013
I do not understand about your "above list of alphabets type" ?
Raman
Raman am 13 Mär. 2013
ooooo sorry sir... i have edited it to block... sorry sorry
Walter Roberson
Walter Roberson am 13 Mär. 2013
You have corrected the length(a) now, as per your recent Question ?
So let a = YourPairsArray(:,1)
where YourPairsArray is the one that has [s,BlockNumber]
Raman
Raman am 13 Mär. 2013
sir can u code few lines for me as m not getting it properly
Walter Roberson
Walter Roberson am 13 Mär. 2013
What is the name of your array that is of the form "[s,blockNumber]" ? Are the values in it stored by columns, so Array(2,1) is the second "s" value?
Raman
Raman am 14 Mär. 2013
i've an array of the form
a=[blockNumber(:), S(:)].'

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Jan
Jan am 14 Mär. 2013
Bearbeitet: Jan am 14 Mär. 2013

0 Stimmen

A simple approach:
S = [2, 10; 5, 16; 1, 16; 7, 16; 4, 23; 6, 31; 3, 31];
v = unique(S(:, 2));
for iv = 1:length(v)
matchIndex = (S(:, 2) == v(iv));
matchBlock = S(matchIndex, 1)
fprintf('%d:', v(iv));
fprintf(' %d', matchBlock);
fprintf('\n');
end
For larger data sets, histc would be much faster. Search for this command in this forum to find more examples.

4 Kommentare

Raman
Raman am 14 Mär. 2013
Bearbeitet: Raman am 14 Mär. 2013
i've tried it on my program but it didnt work...
i've divided the input image into 8x8 blocks and after that i've calculated the mean of each and every block and after that i've sorted the mean values and their corresponding blocks value as
[S,blockNumber]=sort(Mean);
fprintf('\nBlock #%d = %d\n', [blockNumber(:), S(:)].' );
the code is as follows:
a=[blockNumber(:), S(:)].';
len=length(a)-1;
v=unique(S(:));
%disp(v);
for iv=1:length(v)
matchIndex=(S(:)==v(iv));
%disp(matchIndex);
matchBlock=a(matchIndex,1);
fprintf('%d:',v(iv));
fprintf(' %d',matchBlock);
fprintf('\n');
end
error: index exceeds matrix dimension
can u tell me what is the problem in it? i want the output to be like the output produced by your above code...
Jan
Jan am 18 Mär. 2013
Please post the complete error message, especially the line, which causes the error.
Raman
Raman am 19 Mär. 2013
sir i have corrected it....
Jan
Jan am 19 Mär. 2013
@Jassy: Where did you post the complete error message?

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Gefragt:

am 13 Mär. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by