Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Growing matrix dimension mismatch

Subject: Growing matrix dimension mismatch

From: Snow White

Date: 21 Feb, 2011 15:16:07

Message: 1 of 15

Hello,

I have to read some data from a matrix in an almost circular pattern, i am indexing each circle with a counter. The thing is that i am saving the values in a matrix A(:,counter) but i get an error (??? Subscripted assignment dimension mismatch) since the number of values read into A grows each time by increments of 8 values. is there a way to solve this error i have considered preallocation but i can not figure out how to do it. Is there a dynamic way of doing it?

Bye

Subject: Growing matrix dimension mismatch

From: dpb

Date: 21 Feb, 2011 15:30:00

Message: 2 of 15

On 2/21/2011 9:16 AM, Snow White wrote:
> Hello,
>
> I have to read some data from a matrix in an almost circular pattern, i
> am indexing each circle with a counter. The thing is that i am saving
> the values in a matrix A(:,counter) but i get an error (??? Subscripted
> assignment dimension mismatch) since the number of values read into A
> grows each time by increments of 8 values. is there a way to solve this
> error i have considered preallocation but i can not figure out how to do
> it. Is there a dynamic way of doing it?

W/O seeing the actual data and what you're actually doing/trying, not
sure about the bestest way at it but...

If the problem is simply that you're concatenating columns, is the loop
counted? If so, the preallocation is simple enough as

zeros(nRows,maxCounter*8)

if the "grows each time by increments of 8 values" means each group is
another batch of 8 columns.

If you mean you start w/ some number n0 and then each time that goes to
n1=n0+8, n2=n1+8, then the nth iteration is (Counter-1)*8+n0) and the
total is summation to maxCounter which can be precomputed as well.

If that isn't it, show what the actual data is...

--

Subject: Growing matrix dimension mismatch

From: Sean de

Date: 21 Feb, 2011 15:30:31

Message: 3 of 15

"Snow White" wrote in message <ijtvjn$c4r$1@fred.mathworks.com>...
> Hello,
>
> I have to read some data from a matrix in an almost circular pattern, i am indexing each circle with a counter. The thing is that i am saving the values in a matrix A(:,counter) but i get an error (??? Subscripted assignment dimension mismatch) since the number of values read into A grows each time by increments of 8 values. is there a way to solve this error i have considered preallocation but i can not figure out how to do it. Is there a dynamic way of doing it?
>
> Bye

Three easy ways:
-use cell arrays
-Use the third dimension to store the 8 values
-Use counter as a range

for counter = 1:n
   A(:,(counter-1)*8+1:(counter-1)*8+8) = stuff
end

Subject: Growing matrix dimension mismatch

From: James Tursa

Date: 21 Feb, 2011 15:30:31

Message: 4 of 15

"Snow White" wrote in message <ijtvjn$c4r$1@fred.mathworks.com>...
> Hello,
>
> I have to read some data from a matrix in an almost circular pattern, i am indexing each circle with a counter. The thing is that i am saving the values in a matrix A(:,counter) but i get an error (??? Subscripted assignment dimension mismatch) since the number of values read into A grows each time by increments of 8 values. is there a way to solve this error i have considered preallocation but i can not figure out how to do it. Is there a dynamic way of doing it?
>
> Bye

Please show the relevant parts of your code and give some example sizes.

James Tursa

Subject: Growing matrix dimension mismatch

From: ImageAnalyst

Date: 21 Feb, 2011 16:07:02

Message: 5 of 15

Snow White:
I agree completely with the others on the lack of information you
supplied. But, just on a longshot, you mentioned "almost circular
pattern" - depending on what this means, you might find the spiral()
function useful:

spiral

 spiral(n) is an n-by-n matrix with elements ranging
    from 1 to n^2 in a rectangular spiral pattern.

spiral(10)
ans =
    73 74 75 76 77 78 79 80 81 82
    72 43 44 45 46 47 48 49 50 83
    71 42 21 22 23 24 25 26 51 84
    70 41 20 7 8 9 10 27 52 85
    69 40 19 6 1 2 11 28 53 86
    68 39 18 5 4 3 12 29 54 87
    67 38 17 16 15 14 13 30 55 88
    66 37 36 35 34 33 32 31 56 89
    65 64 63 62 61 60 59 58 57 90
   100 99 98 97 96 95 94 93 92 91

They give you the source code also if you want to adapt it.

Another longshot is to use the Euclidean Distance Transform to get you
the radii:
m = zeros(7);
m(4,4) = 1
edt = bwdist(m)

m =
     0 0 0 0 0 0 0
     0 0 0 0 0 0 0
     0 0 0 0 0 0 0
     0 0 0 1 0 0 0
     0 0 0 0 0 0 0
     0 0 0 0 0 0 0
     0 0 0 0 0 0 0
edt =
    4.2426 3.6056 3.1623 3.0000 3.1623 3.6056 4.2426
    3.6056 2.8284 2.2361 2.0000 2.2361 2.8284 3.6056
    3.1623 2.2361 1.4142 1.0000 1.4142 2.2361 3.1623
    3.0000 2.0000 1.0000 0 1.0000 2.0000 3.0000
    3.1623 2.2361 1.4142 1.0000 1.4142 2.2361 3.1623
    3.6056 2.8284 2.2361 2.0000 2.2361 2.8284 3.6056
    4.2426 3.6056 3.1623 3.0000 3.1623 3.6056 4.2426

Subject: Growing matrix dimension mismatch

From: Snow White

Date: 21 Feb, 2011 16:30:23

Message: 6 of 15

dpb <none@non.net> wrote in message <iju0ds$dh8$1@news.eternal-september.org>...
> On 2/21/2011 9:16 AM, Snow White wrote:
> > Hello,
> >
> > I have to read some data from a matrix in an almost circular pattern, i
> > am indexing each circle with a counter. The thing is that i am saving
> > the values in a matrix A(:,counter) but i get an error (??? Subscripted
> > assignment dimension mismatch) since the number of values read into A
> > grows each time by increments of 8 values. is there a way to solve this
> > error i have considered preallocation but i can not figure out how to do
> > it. Is there a dynamic way of doing it?
>
> W/O seeing the actual data and what you're actually doing/trying, not
> sure about the bestest way at it but...
>
> If the problem is simply that you're concatenating columns, is the loop
> counted? If so, the preallocation is simple enough as
>
> zeros(nRows,maxCounter*8)
>
> if the "grows each time by increments of 8 values" means each group is
> another batch of 8 columns.
>
> If you mean you start w/ some number n0 and then each time that goes to
> n1=n0+8, n2=n1+8, then the nth iteration is (Counter-1)*8+n0) and the
> total is summation to maxCounter which can be precomputed as well.
>
> If that isn't it, show what the actual data is...
>
> --

I have tried the following:
conc=[inner_left_pxl inner_top_pxl inner_right_pxl inner_bottom_pxl];
        [ccx,ccy]=size(conc);
        concentric(circle_counter,ccy)=conc;
i still get the following error in the first iteration: ??? Subscripted assignment dimension mismatch.

i can not use pre allocation as i dont know what will be the max value for counter.

bye

Subject: Growing matrix dimension mismatch

From: Snow White

Date: 21 Feb, 2011 16:43:05

Message: 7 of 15

The thing is i have an MxN matrix I want to read data in concentric circles around the centre of the matrix. This is to analyse the data i have.

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <ee7f9de5-61e0-4c31-872f-10b2b278d0cd@z3g2000prz.googlegroups.com>...
> Snow White:
> I agree completely with the others on the lack of information you
> supplied. But, just on a longshot, you mentioned "almost circular
> pattern" - depending on what this means, you might find the spiral()
> function useful:
>
> spiral
>
> spiral(n) is an n-by-n matrix with elements ranging
> from 1 to n^2 in a rectangular spiral pattern.
>
> spiral(10)
> ans =
> 73 74 75 76 77 78 79 80 81 82
> 72 43 44 45 46 47 48 49 50 83
> 71 42 21 22 23 24 25 26 51 84
> 70 41 20 7 8 9 10 27 52 85
> 69 40 19 6 1 2 11 28 53 86
> 68 39 18 5 4 3 12 29 54 87
> 67 38 17 16 15 14 13 30 55 88
> 66 37 36 35 34 33 32 31 56 89
> 65 64 63 62 61 60 59 58 57 90
> 100 99 98 97 96 95 94 93 92 91
>
> They give you the source code also if you want to adapt it.
>
> Another longshot is to use the Euclidean Distance Transform to get you
> the radii:
> m = zeros(7);
> m(4,4) = 1
> edt = bwdist(m)
>
> m =
> 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0
> 0 0 0 1 0 0 0
> 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0
> 0 0 0 0 0 0 0
> edt =
> 4.2426 3.6056 3.1623 3.0000 3.1623 3.6056 4.2426
> 3.6056 2.8284 2.2361 2.0000 2.2361 2.8284 3.6056
> 3.1623 2.2361 1.4142 1.0000 1.4142 2.2361 3.1623
> 3.0000 2.0000 1.0000 0 1.0000 2.0000 3.0000
> 3.1623 2.2361 1.4142 1.0000 1.4142 2.2361 3.1623
> 3.6056 2.8284 2.2361 2.0000 2.2361 2.8284 3.6056
> 4.2426 3.6056 3.1623 3.0000 3.1623 3.6056 4.2426

Subject: Growing matrix dimension mismatch

From: dpb

Date: 21 Feb, 2011 17:45:11

Message: 8 of 15

On 2/21/2011 10:30 AM, Snow White wrote:
> dpb <none@non.net> wrote in message
> <iju0ds$dh8$1@news.eternal-september.org>...
>> On 2/21/2011 9:16 AM, Snow White wrote:
>> > Hello,
>> >
>> > I have to read some data from a matrix in an almost circular pattern, i
>> > am indexing each circle with a counter. The thing is that i am saving
>> > the values in a matrix A(:,counter) but i get an error (??? Subscripted
>> > assignment dimension mismatch) since the number of values read into A
>> > grows each time by increments of 8 values. is there a way to solve this
>> > error i have considered preallocation but i can not figure out how
>> to do
>> > it. Is there a dynamic way of doing it?
>>
>> W/O seeing the actual data and what you're actually doing/trying, not
>> sure about the bestest way at it but...
>>
>> If the problem is simply that you're concatenating columns, is the
>> loop counted? If so, the preallocation is simple enough as
>>
>> zeros(nRows,maxCounter*8)
>>
>> if the "grows each time by increments of 8 values" means each group is
>> another batch of 8 columns.
>>
>> If you mean you start w/ some number n0 and then each time that goes
>> to n1=n0+8, n2=n1+8, then the nth iteration is (Counter-1)*8+n0) and
>> the total is summation to maxCounter which can be precomputed as well.
>>
>> If that isn't it, show what the actual data is...
>>
>> --
>
> I have tried the following:
> conc=[inner_left_pxl inner_top_pxl inner_right_pxl inner_bottom_pxl];
> [ccx,ccy]=size(conc);
> concentric(circle_counter,ccy)=conc;
> i still get the following error in the first iteration: ??? Subscripted
> assignment dimension mismatch.
...

Use a cell array altho I think there's probably a logic flaw unless the
intent is to save the position vector itself and not the data associated
with it; that's unknown as you still didn't provide enough information
to know what you're actual end objective here is.

Re: your other response regarding preallocation--given the naming I'd
presume there's an initial image so there must be some limits to that
such that the computation of the number of passes to get thru should be
computable as long as you don't have some variable stopping criterion
other than the size of the image.

--

Subject: Growing matrix dimension mismatch

From: ImageAnalyst

Date: 21 Feb, 2011 18:41:19

Message: 9 of 15

On Feb 21, 11:43 am, "Snow White" <gulesa...@gmail.com> wrote:
> The thing is i have an MxN matrix  I want to read data in concentric circles around the centre of the matrix. This is to analyse the data i have.
---------------------------------------------------------------------------------
Snow White:
This is so trivial if you have the Image Processing Toolbox:

originalImage = magic(7);
binaryImagePrior = zeros(7);
binaryImagePrior(4,4) = 1
for k = 1 : 3
binaryImage = imdilate(binaryImagePrior, ones(3))
newPixels = binaryImage - binaryImagePrior
[rows cols] = find(newPixels);
% Now you know all the rows and columns
% of the new outer circle!
% Now you can take the mean of the original
% image or whatever you want to do.
binaryImagePrior = binaryImage;
end

That's it. It doesn't matter if you consider your array an image or
not - it doesn't matter one bit. If you don't have the IPT, then you
can easily find morphological dilation code on the internet.

I can't do any more because you didn't define what you mean by "read
data." Do you simply want to list the values of the data in the
command window, or do you actually want to do something with them,
like take their mean, max, or whatever.
ImageAnalyst

Subject: Growing matrix dimension mismatch

From: Snow White

Date: 21 Feb, 2011 18:51:04

Message: 10 of 15

 conc=[bottom_left_diag_pxl left_pxl top_left_diag_pxl top_pxl top_right_diag_pxl right_pxl bottom_right_diag_pxl bottom_pxl];
            [ccx,ccy]=size(conc)
            concentric=zeros(1,ccy);
            concentric=conc;
I have written the above code, the things is that concentric rewrites all the values, i know that it because of using the zeros command but i want it replace just the current values not the whole set.

dpb <none@non.net> wrote in message <iju8bc$etr$1@news.eternal-september.org>...
> On 2/21/2011 10:30 AM, Snow White wrote:
> > dpb <none@non.net> wrote in message
> > <iju0ds$dh8$1@news.eternal-september.org>...
> >> On 2/21/2011 9:16 AM, Snow White wrote:
> >> > Hello,
> >> >
> >> > I have to read some data from a matrix in an almost circular pattern, i
> >> > am indexing each circle with a counter. The thing is that i am saving
> >> > the values in a matrix A(:,counter) but i get an error (??? Subscripted
> >> > assignment dimension mismatch) since the number of values read into A
> >> > grows each time by increments of 8 values. is there a way to solve this
> >> > error i have considered preallocation but i can not figure out how
> >> to do
> >> > it. Is there a dynamic way of doing it?
> >>
> >> W/O seeing the actual data and what you're actually doing/trying, not
> >> sure about the bestest way at it but...
> >>
> >> If the problem is simply that you're concatenating columns, is the
> >> loop counted? If so, the preallocation is simple enough as
> >>
> >> zeros(nRows,maxCounter*8)
> >>
> >> if the "grows each time by increments of 8 values" means each group is
> >> another batch of 8 columns.
> >>
> >> If you mean you start w/ some number n0 and then each time that goes
> >> to n1=n0+8, n2=n1+8, then the nth iteration is (Counter-1)*8+n0) and
> >> the total is summation to maxCounter which can be precomputed as well.
> >>
> >> If that isn't it, show what the actual data is...
> >>
> >> --
> >
> > I have tried the following:
> > conc=[inner_left_pxl inner_top_pxl inner_right_pxl inner_bottom_pxl];
> > [ccx,ccy]=size(conc);
> > concentric(circle_counter,ccy)=conc;
> > i still get the following error in the first iteration: ??? Subscripted
> > assignment dimension mismatch.
> ...
>
> Use a cell array altho I think there's probably a logic flaw unless the
> intent is to save the position vector itself and not the data associated
> with it; that's unknown as you still didn't provide enough information
> to know what you're actual end objective here is.
>
> Re: your other response regarding preallocation--given the naming I'd
> presume there's an initial image so there must be some limits to that
> such that the computation of the number of passes to get thru should be
> computable as long as you don't have some variable stopping criterion
> other than the size of the image.
>
> --

Subject: Growing matrix dimension mismatch

From: ImageAnalyst

Date: 21 Feb, 2011 19:07:46

Message: 11 of 15

On Feb 21, 1:51 pm, "Snow White" <gulesa...@gmail.com> wrote:
> I have written the above code, the things is that concentric rewrites all the values, i know that it because of using the zeros command but i want it replace just the current values not the whole set.
-----------------------------------------------
Then do it like I showed you - using imdilate.
ImageAnalyst

Subject: Growing matrix dimension mismatch

From: Snow White

Date: 21 Feb, 2011 19:11:05

Message: 12 of 15

Thank you, i dont completely understand the approach that you have suggested.

I have the following matrix: y=rand(7,7), i want to find the values that lie on the concentric circle(reading one pixel depth on the perimeter of the circle).

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <c7bae3ab-9a10-4648-8d1c-1f6dad33bb51@x1g2000yqb.googlegroups.com>...
> On Feb 21, 11:43 am, "Snow White" <gulesa...@gmail.com> wrote:
> > The thing is i have an MxN matrix  I want to read data in concentric circles around the centre of the matrix. This is to analyse the data i have.
> ---------------------------------------------------------------------------------
> Snow White:
> This is so trivial if you have the Image Processing Toolbox:
>
> originalImage = magic(7);
> binaryImagePrior = zeros(7);
> binaryImagePrior(4,4) = 1
> for k = 1 : 3
> binaryImage = imdilate(binaryImagePrior, ones(3))
> newPixels = binaryImage - binaryImagePrior
> [rows cols] = find(newPixels);
> % Now you know all the rows and columns
> % of the new outer circle!
> % Now you can take the mean of the original
> % image or whatever you want to do.
> binaryImagePrior = binaryImage;
> end
>
> That's it. It doesn't matter if you consider your array an image or
> not - it doesn't matter one bit. If you don't have the IPT, then you
> can easily find morphological dilation code on the internet.
>
> I can't do any more because you didn't define what you mean by "read
> data." Do you simply want to list the values of the data in the
> command window, or do you actually want to do something with them,
> like take their mean, max, or whatever.
> ImageAnalyst

Subject: Growing matrix dimension mismatch

From: ImageAnalyst

Date: 21 Feb, 2011 20:11:37

Message: 13 of 15

On Feb 21, 2:11 pm, "Snow White" <gulesa...@gmail.com> wrote:
> Thank you, i dont completely understand the approach that you have suggested.
>
> I have the following matrix: y=rand(7,7), i want to find the values that lie on the concentric circle(reading one pixel depth on the perimeter of the circle).
----------------------------------------------------------------------------------
I think I understand you perfectly. That's what the code does. Look
again. Here I list all the pixel values in the concentric "ring" and
take their mean. But you need to realize that with any quantized
array, a circle is not so precisely defined, since there are pixels
that will be at some radius (like 1) and there are some gaps (because
the diagonal pixels one layer out would be at sqrt(2) so you have to
decide whether you want to include those or not. In other words, you
have to decide on the connectivity. And this is true for *whatever*
algorithm you decide on.
Hopefully you'll understand more after studying this code.

originalImage = magic(7)
binaryImagePrior = false(7);
binaryImagePrior(4,4) = true
kernel = [0 1 0; 1 1 1; 0 1 0]; % 4-connectivity.
% Or you can use 8-connectivity.
kernel = ones(3); % 8-connectivity.
for k = 1 : 3
binaryImage = imdilate(binaryImagePrior, kernel)
newPixels = logical(binaryImage - binaryImagePrior)

% List the pixel values in the command window:
originalImage(newPixels)

% For fun, let's also get their rows and columns.
         % although it's not really needed or used here.
[rows cols] = find(newPixels);

% Now you know all the rows and columns
% of the new outer circle!
% Now you can take the mean of the original
% image or whatever you want to do.
meanValue = mean(originalImage(newPixels))

        % The current binary image is now the old one.
binaryImagePrior = binaryImage;
end

Subject: Growing matrix dimension mismatch

From: Snow White

Date: 22 Feb, 2011 18:04:20

Message: 14 of 15

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <239f2a0a-2a82-4ab4-9740-9d0b48cdd9b4@s11g2000yqh.googlegroups.com>...
> On Feb 21, 2:11 pm, "Snow White" <gulesa...@gmail.com> wrote:
> > Thank you, i dont completely understand the approach that you have suggested.
> >
> > I have the following matrix: y=rand(7,7), i want to find the values that lie on the concentric circle(reading one pixel depth on the perimeter of the circle).
> ----------------------------------------------------------------------------------
> I think I understand you perfectly. That's what the code does. Look
> again. Here I list all the pixel values in the concentric "ring" and
> take their mean. But you need to realize that with any quantized
> array, a circle is not so precisely defined, since there are pixels
> that will be at some radius (like 1) and there are some gaps (because
> the diagonal pixels one layer out would be at sqrt(2) so you have to
> decide whether you want to include those or not. In other words, you
> have to decide on the connectivity. And this is true for *whatever*
> algorithm you decide on.
> Hopefully you'll understand more after studying this code.
>
> originalImage = magic(7)
> binaryImagePrior = false(7);
> binaryImagePrior(4,4) = true
> kernel = [0 1 0; 1 1 1; 0 1 0]; % 4-connectivity.
> % Or you can use 8-connectivity.
> kernel = ones(3); % 8-connectivity.
> for k = 1 : 3
> binaryImage = imdilate(binaryImagePrior, kernel)
> newPixels = logical(binaryImage - binaryImagePrior)
>
> % List the pixel values in the command window:
> originalImage(newPixels)
>
> % For fun, let's also get their rows and columns.
> % although it's not really needed or used here.
> [rows cols] = find(newPixels);
>
> % Now you know all the rows and columns
> % of the new outer circle!
> % Now you can take the mean of the original
> % image or whatever you want to do.
> meanValue = mean(originalImage(newPixels))
>
> % The current binary image is now the old one.
> binaryImagePrior = binaryImage;
> end

The thing is i want to know how to store originalImage(newPixels) in a structure that is indexed with respect to k?

Subject: Growing matrix dimension mismatch

From: ImageAnalyst

Date: 22 Feb, 2011 18:19:31

Message: 15 of 15

On Feb 22, 1:04 pm, "Snow White" <gulesa...@gmail.com> wrote:
> The thing is i want to know how to store originalImage(newPixels) in a structure that is indexed with respect to k?
-------------------------------------------------------------------------------------
Well the number of points varies as you change radius. So how about a
cell array, something like
ca(k) = {originalImage(newPixels)};

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us