Solved

# Problem assigning columns indirectly, ??? Subscripted assignment dimension mismatch

Posted on 2009-02-21
2,314 Views
Hi all,

I'm having a major problem that doesn't make any sense to me. Basically, I have an array x2 = [4;5;6], a scalar N = 3, an array of zeros size 7 * 7 called b, and an array m = [1 2 3 4 5 6 7]. I want to iterate through m and assign the columns of b like my code shows. What I am doing wrong, and how can I achieve this?

Keep in mind that when I tell matlab to assign columns individually by using a number 1 through 7 in place of "m" the column is assigned correctly. It is just when I try to assign all of them with one command that it tells me, "??? Subscripted assignment dimension mismatch"
``````N = 3;

x2 = [4;5;6];

b = zeros(3*N-2,3*N-2);

m = 1:(3*N-2);

b(:,m) = [zeros(m-1,1);x2;zeros(2*N-1-m,1)]

??? Subscripted assignment dimension mismatch
``````
0
Question by:ehensens

LVL 15

Expert Comment

First, even if you assign this in a for-loop, you can do it only until m=5. For m>5, you get vector with more elements then number of rows in b. This is probably why you have this error.
Second, you cannot use vector as an argument for zeros function. See its documentation. b(:,m) is just the same as b, since m lists indices for all the columns.
If you want the code, please explain how it should behave when m>5. What matrix you want as a result?
0

LVL 5

Expert Comment

The reason you're getting a subscript dimension mismatch is that you're trying to assign a 2-dimensional vector to the second dimension of another 2-dimensional vector.  Instead, try doing the following:

b(:,m(:)) = [zeros(m(:)-1,1);x2;zeros(2*N-1-m(:),1)]

Not sure if it will work, but give it a try.  As for b having only 5 rows, I don't see it.  I think that's ok.

Luke
0

LVL 15

Expert Comment

LukeyJay,
As I said you cannot use vector/matrix as an argument for zeros function, only scalars are accepted, or only one argument as a size vector. If you use vector as one of the arguments, zeros will use only the first element (and give you a warning).
I didn't said b has 5 rows, it has 7 rows. But at m=6 or 7, vector on the right side of assignment will have length 8 or 9. This is where the error comes from.

To vectorize this command, it should be rewritten completely.

May be this is what ehensens wants? But I need more clarification.
0

LVL 15

Expert Comment

For some reason the code didn't attach. Here is it.
``````a=repmat([x2;zeros(5,1)],1,7);

b=reshape(a(1:49),7,7)
``````
0

LVL 5

Expert Comment

yuk99,

You're right, b would be the wrong size.  The equation for the last several rows leads to indeces 0 and -1 and -2 for m = 5 and m = 6 and m = 7, respectively, which would be invalid indeces anyway.  So, as you point out, there's no way of knowing what to do unless further clarification is given.  Probably, I would guess, he/she wants the values of x2 that wouldn't "fit" in the declaration to be truncated off.

By the way, my code didn't work, but in all fairness to myself, I didn't have matlab in front of me at the time.  It was a shot in the dark, and it missed.

Luke
0

Author Comment

Thanks all for the comments; I understand that I need to be more detailed in what I want. Here it is:

If I have a 1 dimensional matrix of any size (see code snippet), I want to make a matrix that looks like the second matrix in my code snippet, but the key is WITHOUT any loops.

I hope you can all understand how that was my intention from my original post, but I just don't know how to do it.

Thanks for any help!

``````a = [1;2;3]

b = 1 0 0 0 0 0 0

2 1 0 0 0 0 0

3 2 1 0 0 0 0

0 3 2 1 0 0 0

0 0 3 2 1 0 0

0 0 0 3 2 1 0

0 0 0 0 3 2 1

0 0 0 0 0 3 2

0 0 0 0 0 0 3

Where a is a matrix of size N and b is a matrix of size (3N-2)x(3N)
``````
0

Author Comment

sorry I meant size (3N)x(3N-2)
0

LVL 15

Accepted Solution

One of the solutions is below. There was one problem in the task. If we assume that number of rows in matrix b depends on N (3N), then number of rows (3N-2) is true only for N=3. The general case will be 3N-N+1. I used a(:) in repmat just to make sure a is column vector.

You can also modify the code in my previous comment, it would be another solution.
``````N = 3;

a = 1:N;

r=3*N;

c=3*N-N+1;

b = zeros(r,c);

idx=tril(ones(r,c))-tril(ones(r,c),-N);

b(idx>0)=repmat(a(:),c,1);
``````
0

Author Closing Comment

Thank you very much.
0

Author Comment

Wait,

Upon further inspection I'm not positive this is the correct solution. The input matrix is not always [1;2;3], it can be any size and can contain any numbers. Where in your solution does it take the input matrix?
0

LVL 15

Expert Comment

Input is variable a. I assume this is vector with the length N. Correct?
I used a=1:N only as an example.
0

Author Comment

Very well, thank you once more and nice job.
0

## Featured Post

### Suggested Solutions

sameEnds challenge 3 82
why to use string 10 53
fix34  challenge 9 70
Change to event 1 49
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…