How to fix the error code 'Subscripted assignment dimension mismatch.'

Here is the code that I'm getting the error:
for z=1:length(X)
V(z)=[X(z):Y];
end
where X is a 1x81 matrix and Y is an integer

回答 (2 件)

Ameer Hamza
Ameer Hamza 2018 年 5 月 9 日

0 投票

The right side of the assignment is an array, you are trying to assign to one vector element V(z). This is not possible with matrices in MATLAB. You will need cell array for this,
V = cell(1, length(X));
for z=1:length(X)
V{z}=[X(z):Y];
end

3 件のコメント

Davis Storey
Davis Storey 2018 年 5 月 9 日
Is there a way to do this without using cell arrays? If not how to u divide a matrix by a cell array?
Ameer Hamza
Ameer Hamza 2018 年 5 月 9 日
Can you show your data? How do you want to divide. You can convert cell array to matrix using cell2mat() but that will only work if all elements of V have same length.
Walter Roberson
Walter Roberson 2018 年 5 月 9 日
編集済み: Walter Roberson 2018 年 5 月 9 日
In order to do it without using cell arrays, you would need to pad the array so that all of the rows were the same length.
What is the matrix that is to be divided? And are you wanting to do element-by-element division or to do the equivalent of the inverse of algebraic matrix multiplication? Are you trying to do something like solve a sparse system of linear equations?

サインインしてコメントする。

Davis Storey
Davis Storey 2018 年 5 月 9 日
編集済み: Walter Roberson 2018 年 5 月 9 日

0 投票

after finding v, I need to input it into another for loop:
for k=1:length(v)
p34{k}=P*(X./v(k)).^constant;
end

1 件のコメント

Walter Roberson
Walter Roberson 2018 年 5 月 9 日
No, you cannot expect to do that. X is a fixed size vector, but v is a variable sized vector unless all of the entries in X are the same. When you X ./ a variable-sized subset of X, you are going to have size inconsistencies. This does not make sense unless you want to divide every element of X by every element of v, making a 2D matrix for each p34 element.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2018 年 5 月 9 日

コメント済み:

2018 年 5 月 9 日

Community Treasure Hunt

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

Start Hunting!

Translated by