??? Subscripted assignment dimension mismatch.

조회 수: 5 (최근 30일)
Fred John
Fred John 2014년 11월 23일
댓글: Fred John 2014년 11월 23일
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
  댓글 수: 1
Azzi Abdelmalek
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 CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by