I am basically trying to write a subroutine where I loop over N vectors called rij and then create a 3*3N array by evaluating each vector with a function which returns a 3*3 matrix, these 3*3 matrices then being putting next to each other in an array until one ends up with a 3*3N array after looping for the N rij vectors. (To clarify, p is a 3x1 column and sing is a 3x100 array which I am viewing as an array of 100 3x1 column vectors.

However, I can't get the matrix dimensions to agree for the matrix FF when I try this, how should I be assigning the indices for the matrix in order to be able to achieve this?

function[v]=StokesletVelocity(N,sing,p,Kn,f)

for i=1:N

rij= (p - sing(1:3,i));

FF(1:3,1:i)=((1/8*pi*Kn)*((eye(3)/norm(rij)) + (rij'*rij)/norm(rij)^3))'; %subroutine to find velocity at the point %

end

v=FF*f;

end

Answer by Guillaume
on 16 Aug 2019

for i=1:N

FF(1:3,1:i) = ...

So, clearly on the first iteration, this means assign something to:

FF(1:3, 1:1) = ... %assign to a 3x1 matrix

on the 2nd iteration:

FF(1:3, 1:2) = ... %assign to a 3x2 matrix. 1 more row than before

etc. Each time you try to assign to something with one more column (also overwriting what was assigned previously).

Of course, if the something is 3x3, assigning it to a 3x1 array is not going to work. Haven't we discussed this with you before?

Now, you could play around with indices, maybe this is what you intended to do:

FF = zeros(3, N*3); %always preallocate your arrays rather than growing in a loop

for i = 1:N

FF(:, (1:3) + 3*(i-1)) = ...

end

But since you seem to be struggling with indexing, it may be simpler to use a cell array as in your previous question, then concatenate everything:

FF = cell(1, N);

for i = 1:N

FF{i} = ...

end

FF = [FF{:}];

Tom
on 16 Aug 2019

I am trying the following which creates something of the right size

for i=1:100

rij= (p - sing(1:3,i));

FF(1:3, 3*i-2:3*i) =((1/8*pi*Kn)*((eye(3)/norm(rij)) + (rij'*rij)/norm(rij)^3))'; %subroutine to find velocity at the point %

end

although not when I put it in a function to try and get a subroutine for some reason. Also looks like some strange repetitions so not sure if the loop does exactly what I require.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## KALYAN ACHARJYA (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476290-matrix-dimension-in-subroutine#comment_735454

## Tom (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/476290-matrix-dimension-in-subroutine#comment_735740

Sign in to comment.