Link to home
Start Free TrialLog in
Avatar of ehensens
ehensensFlag for United States of America

asked on

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

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

Open in new window

Avatar of yuk99
yuk99
Flag of United States of America image

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?
Avatar of LukeyJay
LukeyJay

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
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.
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)

Open in new window

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
Avatar of ehensens

ASKER

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)

Open in new window

sorry I meant size (3N)x(3N-2)
ASKER CERTIFIED SOLUTION
Avatar of yuk99
yuk99
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you very much.
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?
Input is variable a. I assume this is vector with the length N. Correct?
 I used a=1:N only as an example.
Very well, thank you once more and nice job.