Sequence of submatrix based on the position of a rectangle

14 views (last 30 days)
I found the method to derive the submatrix (based on the position of the rectangle that I select through imagesc-> Insert-> rectangle) from the original matrix. Follow the link:
But now I have a sequence of matrices, not just one, and I should find the respective submatrices selecting only that rectangle. How can i do?
  3 Comments
Walter Roberson
Walter Roberson on 16 Oct 2011
Duplicate is at http://www.mathworks.com/matlabcentral/answers/17973-block-to-make-a-loop-to-find-a-sequence-of-submatrix

Sign in to comment.

Answers (8)

Vincenzo
Vincenzo on 16 Oct 2011
I load all the matrices, then on the first matrix I do imagesc-> Insert-> rectangle ... after I click with right mouse button shows M code, which gives me the position, respectively [left bottom width height]. Due to the location, I have the coordinates to calculate the single submatrix but I would like to calculate the other. Submatrix is Usally Submatrix= NameOriginalMatrix =(row1: row2, column1: column2) But if i have 10 matrices with the same number of rows and columns (matrix1, matrix2,.... Matrix10), in output I would like to automatically calculate the other like this:
submatrix1= matrix1 (row1: row2, column1: column2);
submatrix2 =matrix2 (row1: row2, column1: column2);
. . . submatrix10 =matrix10 (row1: row2, column1: column2);
All this only with the position of a rectangle
  2 Comments
Walter Roberson
Walter Roberson on 16 Oct 2011
http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
Image Analyst
Image Analyst on 16 Oct 2011
I don't see a question. You've stated what you need, and given a solution to it in only 10 lines, so as I see it, you're all set.

Sign in to comment.


Vincenzo
Vincenzo on 18 Oct 2011
No, i'm not all set.
What i think i should do:
1)Load all the matrices
2)Write in a .mat or another kind of file the following things:
% [x1 y1 width hight] coordinates of part of image i selected
x1=...; y1=...; x2=x1+width; y2=y1+hight;
[rows columns numberOfColorChannels]=size(NameOfAMatrix);
row1=int32(x1*columns);
row2=int32(x2*columns);
column1=int32(y1*rows);
column2=int32(y2*rows);
N=500 ; % number of matrices
for i=1:N
sub(i)=Matrix(i)(row1:row2,column1:column2)[1]*
end %end of file
3)just call the file[2*]
[1*]The real problem is to implement automatically the matrices...the name are Seq1,...,Seq500, so if i do sub(i)=Seq(i)(row1:row2,column1:column2) i call all the matrices or not?
[2*]What kind of file?
The 3 steps are right or not?

Image Analyst
Image Analyst on 18 Oct 2011
In (2), you're getting rows and columns confused. x is columns and y is rows. So this
row1=int32(x1*columns);
is meaningless. I have no idea what you're asking in (3). The code you tried to write in 2 should be put into an "m-file" with the text editor. Then you'd run that. .mat files are usually for storing data (variables) and never used for storing source code. Seems like you need to go through the Getting Started guide.

Vincenzo
Vincenzo on 18 Oct 2011
Yes..i want to write column1=int32(x1*columns) and row1=int32(y1*rows) ;)
The syntax for the .m file i see it's not all right
What i write:
N=10;
x1=0.4196; y1=0.6238; width=0.1446; hight=0.1286; x2=x1+width; y2=y1+hight;
[rows columns numberOfColorChannels]=size(Seq9);
column1=int32(x1*columns);
column2=int32(x2*columns);
row1=int32(y1*rows);
row2=int32(y2*rows);
for i=1:N
sub{i}=Seq{i}(row1:row2 ,column1:column2); %Here is the error
i=i+1;
end
but when i call >>name_of_file it says to me: "??? Undefined variable "Seq" or class "Seq". "
I write Seq only because i want to automatically implements Seq1,...,Seq10(name of matrices)..what's wrong?
Please I need your help!!!

Image Analyst
Image Analyst on 18 Oct 2011
I don't think you can do this Seq{i}(row1:row2 ,column1:column2); first of all, you'd need to have Seq previously defined and created, which I don't think you have, based on the error message. Then, assuming you did stuff an array into Seq{i} I'd extract it like this
m = Seq{i}; sub{i} = m(row1:row2, column1:column2);
Finally, incrementing i (i=i+1) inside a for loop does nothing - it ignores it and follows the iteration set up on the "for" line. Changing the iterator ("i") inside the loop as no effect on the number of iterations that take place, as discussed in this forum before.

Vincenzo
Vincenzo on 19 Oct 2011
I add:
[rows ....numberOf...]=size (Seq); %[1*]
.
.
for i=1:N
m=Seq{i};
sub{i}=m(row1:row2, column1:column2); %[2*]
end
i=i+1;
% these are the messages:
%[1*] Variable 'Seq' is used, but apparently not set
%[2*] sub might be growing inside a loop.Consider preallocating for speed
I declared in the workspace Seq, but when you say "... Then, assuming you did stuff an array into Seq{i}.." what i have to do exactly? I don't understand

Vincenzo
Vincenzo on 20 Oct 2011
I have the following matrices Seq001...Seq009,Seq010..Seq099,Seq100...Seq999
what i did:
% namefile.m
N=999;
x1=0.4196;
y1=0.6238;
width=0.1446;
hight=0.1286;
x2=x1+width;
y2=y1+hight;
[rows columns numberOfColorChannels]=size(Seq009);
column1=int32(x1*columns);
column2=int32(x2*columns);
row1=int32(y1*rows);
row2=int32(y2*rows);
name='Seq';
sub='sub';
subunit=sub+'0'+'0';
subdecina=sub+'0';
for i=1:N
if (i<=9)
unit=name+'0'+'0'+'i'; % from Seq001 to Seq009
subunit1=subunit+'i'; [*] % from sub001 to sub009
subunit1=unit(row1:row2 ,column1:column2); % line 38
end
if (i>=10 && i<=99)
decina=name+'0'+'i'; % from Seq010 to Seq099
subdecina1=subdecina+'i'; [**] % from sub010 to sub099
subdecina1=decina(row1:row2 ,column1:column2);
end
if(i>=100 && i<=999)
centinaia=nome+'i'; % from Seq100 to Seq999
subcentinaia=sub+'i'; [***] % from sub100 to sub999
subcentinaia=centinaia(row1:row2 ,column1:column2);
end
end
i=i+1;
...now if i do>> namefile it says:
" ??? Index exceeds matrix dimensions. at line 38"
and in .m file i have 3 warnings in the lines that i marked with [*],[**],[***] having the same advice: "The value assigned here to variable 'subunit1'/'subdecina1'/'subcentinaia' might be never used"
Why?? It looks to me right but it's not.

Vincenzo
Vincenzo on 21 Oct 2011
Is there a string concatenation method in wich i can recall the matrices?...Because i need a dynamic vector containing all the matrices(bu i don't want write all statically)..Maybe a method which i can concatenate:
name='Seq';
number1='0';
number2='0'
N=5;
for i=1:N
example=name+number1+number2+i
%here i would that appear Seq001,...,Seq005 that are linked with the matrices Seq001,...,Seq005 but indeed it appears example='Seq00'..just a string and not the matrix...how can i do?
Please someone can help me?? I have no idea!!!
end
i=i+1;
  2 Comments
Vincenzo
Vincenzo on 21 Oct 2011
Or call always in a .m file all the matrices that are in a folder: C:\Desktop\Folder\... is it possible?

Sign in to comment.

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!