MATLAB Answers

Trying to create a multidimensional array

2 views (last 30 days)
Josh Rizzolo
Josh Rizzolo on 3 Aug 2021
Commented: Josh Rizzolo on 3 Aug 2021
I am trying to implement the formula below, where each is an element of the larger ψ vector of length g.
So far I have this
psi = zeros(1,g);
for i = 1:g
psi(i) = [zeros(h,(i-1)) eye(h) zeros(h, (N-i-h+1))]';
but I receive the error
"Unable to perform assignment because the left and right sides have a different number of elements."
which makes sense. Is there a way to do what I am attempting, or am I SOL?

Accepted Answer

Dave B
Dave B on 3 Aug 2021
I'm not 100% sure I follow the goal, the code you've pasted appears to make g matrices, each with h columns and the number of rows is (i-1) + h + max((N-i-h+1),0)...(I think?)
What shape would you like the result to take? To store the matrices separately, you can use a cell array:
psi = cell(1,g);
for i = 1:g
psi{i} = [zeros(h,(i-1)) eye(h) zeros(h, (N-i-h+1))]';
psi = 1×3 cell array
{10×4 double} {10×4 double} {10×4 double}
If you're using h,g,N that produce consistent numbers of rows (as in the above case) you could store this in a 3-d matrix
psi = zeros(N,h,g);
for i = 1:g
psi(:,:,i) = [zeros(h,(i-1)) eye(h) zeros(h, (N-i-h+1))]';
ans = 1×3
10 4 3
  1 Comment
Josh Rizzolo
Josh Rizzolo on 3 Aug 2021
Thanks for getting back to me so fast.
Being completely honest: I'm not 100% on what this array means or how it's supposed to look. The code was basically trying to assemble g matrices of the form given. But, the paper I'm pulling the formula from is certainly trying to concatenate them all into a matrix to do math with.
Constants g, N, and h will all fluctuate depending on the context of the function, hence why they aren't predefined in the code (g = N - h + 1 as a quick aside).
I completely spaced on the proper syntax for putting 2D matrices into 3D ones, so I will try making the adjustment in your second suggestion first. I still anticipate problems stemming from the varying dimensions however.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by