how can i store the location (x,y) in a variable in a loop?

the code i tried is:
index=1;
B=zeros(BmatRows,BmatCols);
for row=1:incrR:rows-(blockSizeR-1)
rowStart=row;
rowEnd=row+(blockSizeR-1);
for col=1:incrC:columns-(blockSizeC-1)
colStart=col;
colEnd=col+(blockSizeC-1);
oneBlock=grayImage(rowStart:rowEnd,colStart:colEnd);
vecOneBlock= reshape(oneBlock,1,numel(oneBlock));
B(index,:)=vecOneBlock;
index=index+1;
end
end
[B,index]=sortrows(B);
here i want to track the rowstart and colstart as a point location like (rowstart,colstart) stores in a variable and changed according to the loop after that.
the matrix B is now according to the "sortrows" function and the index is also according to the matrix B. i.e the "index" values also shuffles.
i want that the after storing point values in a variable it also shuffles according to the matrix B.
i dont know how to do this plz help me to do so...

 Accepted Answer

hi,
According to your program try this :
%BmatRows=140;
%BmatCols=150;
%blockSizeR=4;
%blockSizeC=5;
%incrR=1;
%incrC=1;
%rows=40;
%columns=40;
% THE VALUES ABOV ARE CREATED FOR TESTING PURPOSE
index=1;
B=zeros(BmatRows,BmatCols);
for row=1:incrR:rows-(blockSizeR-1)
rowStart=row;
rowEnd=row+(blockSizeR-1);
for col=1:incrC:columns-(blockSizeC-1)
colStart=col;
TRACKING(index,:)=[rowStart colStart];
colEnd=col+(blockSizeC-1);
oneBlock=grayImage(rowStart:rowEnd,colStart:colEnd);
vecOneBlock= reshape(oneBlock,1,numel(oneBlock));
B(index,:)=vecOneBlock;
index=index+1;
end
end

More Answers (1)

Inside the innermost loop, add these lines:
starts(index, 1) = rowStart;
starts(index, 2) = colStart;
Or as a single line like
starts(index, 1:2) = [rowStart, colStart];

6 Comments

sir how the sorting of starts is done? like
in my above code if
B=[ 23 45 67 89 index=[1 starts= [ 1 1
10 33 67 98 2 1 2
87 34 21 56 3 1 3
21 34 56 78] 4] 1 4 ]
here, the first row corresponding to one block 2x2 of the image arranged in a vector.
After applying the "sortrows" function
B=[ 10 33 67 98 index=[2 starts=[ 1 2
21 34 56 78 4 1 4
23 45 67 98 1 1 1
87 34 21 56] 3] 1 3 ]
i want that "starts" also changed accordingly as i told u in above example. how this can be done..
if i wrote
[B,index,starts]=sortrows(B);
it gives error and says too many arguments....
plz help me...
Is that really "oneBlock" instead of B, because after one row, B should have only one row, not 4.
And sortrows returns only 2 arguments, at most, not 3. And even if it did, you would totally trash the index and starts arrays that you took the trouble to create. Why would you do that?
Do you want this:
[B, sortingIndex] = sortrows(B);
% Re-order starts in the same way as B.
starts = starts(sortingIndex);
sir like if
oneBlock = [ 12 34
67 54]
then i stored it as vector like this [12 67 34 54]. the "index i 've used corresponds to the blockNumber mainly...
when the image has been scanned from top to bottom, i've the matrix B, their index values and their location "starts".
after applying sortrows the index is also changed now i want the location that is stored in starts also get changed so that i can use it further
starts=starts(sortingIndex);
shows only a single values like
[ 104
102
45
78
46 ..... and so on]
i want this as
[ 1 104
1 102
5 45 .. and so on ]
so that i can recognize the starting rowstart and colstart values as point location....
as u told me earlier
starts(index,1:2)=[rowstart,colstart]
i want to sort the values of starts in pairs
Sorry - forgot that starts was a 2D array and needs 2 indexes. Try it this way:
starts=starts(sortingIndex, :);
it works thank you so much sir

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!