??? Subscripted assignment dimension mismatch.

3 次查看(过去 30 天)
Getting the error in title, my code was:
>> P=zeros(3,3);
>> P(3,3)=p
Any idea why? I'm guessing I need to assign p to something. But p will be my input value for a function.

采纳的回答

Geoff Hayes
Geoff Hayes 2014-11-23
Fred - the error message is telling you that there is a dimension mismatch between the variables that are being assigned at
P(3,3) = p;
As P is a 3x3 matrix, then P(3,3) is a scalar (so 1x1 element). If p is a scalar, then the above will work. But if p is an array or matrix (so anything other than a 1x1) then you will observe this error.
For example, consider the following code
P=zeros(3,3);
p = 42;
P(3,3) = p; % scalar assignment works
p = [42 42];
P(3,3) = p; % assignment of vector to scalar fails
P(3,1:2) = p; % assignment of vector to a vector works
So the dimensions of what you are trying to assign (the "source") must match the dimensions of the "destination". So what are you trying to assign to this element of P? What are the dimensions of p?
  18 个评论
Geoff Hayes
Geoff Hayes 2014-11-23
Fred - what format is the data in now? If it is just in a text file, perhaps a comma separated file with three columns for a, b, and c, then you can use importdata or csvread. Start with this.
Once you have read in your data, presumably into a 20x3 matrix, you can then iterate over each row of the matrix and extract the three different values. You would then pass these three values into your function, and store the result somewhere. Whether you would "store" this in P(3,3) remains to be seen. Perhaps you will have 20 such P matrices.
Fred John
Fred John 2014-11-23
Yes, 20 different matrices of P. I'll have a crack at that and see what happens. This is the beginning of my code so I'll probably be posting more questions!
Thanks Geoff

请先登录,再进行评论。

更多回答(1 个)

Azzi Abdelmalek
Azzi Abdelmalek 2014-11-23
This is not clear what you want to do. You have to assign some value to p
p=4
P=zeros(3,3);
P(3,3)=p

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by