??? 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.
0 个评论
采纳的回答
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
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.
更多回答(1 个)
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
1 个评论
Azzi Abdelmalek
2014-11-23
It's not clear what you want, maybe you need to use cell array
p=[2 4;8 7];
P=cell(3,3);
P{3,3}=p
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!