??? Subscripted assignment dimension mismatch.

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일

0 개 추천

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

Fred John
Fred John 2014년 11월 23일
Thanks Geoff. I wish to define the p value as the output of a function. The input to this function will have 3 variables, and those 3 variables will come from a data source.
So the question is how do I tell Matlab that this p value is the output of a function, of which the inputs are from some data source?
But what do you expect p to be - a scalar, an array, or a matrix? You don't have to tell MATLAB that p is the output of a function, as you could easily do
P(3,3) = myFunc(A,B,C);
But the above will only work if the output from myFunc is a scalar.
Fred John
Fred John 2014년 11월 23일
I expect p to be a value between 0 and 1; a probability.
If I do it that way, it tells me the input variable A is undefined. As I said the inputs will be used from an external data source.
Fred - the above is just an example, so of course trying to execute it will generate an error. And myFunc is probably not even the name of a function that you have.
If p is in fact a value between 0 and 1, then calling
P(3,3) = p;
should work. But since it doesn't, it is because p is not a scalar like you think it is. Try calling
size(p)
before the assignment. What does this return?
size(p)
ans =
17 17
no I tried that with my own function, and it did not work either. size(p) gives above.
If the size of p is [17 17] how can you write?
P(3,3) = p
Sorry, let me clarify what I'm trying to do:
P = zeros(3,3)
generates a 3x3 matrix of zeros. Then,
P(3,3) = p
assigns the (3,3) element to be p. But error is I'm trying/need to define p. For example if I write:
P(3,3) = 1/2
instead, it returns a matrix of zeros with the (3,3) element being 0.5. But I want the (3,3) element to be p, which should be generated from a function with inputs from an external data set. Does that make sense?
You said the size of p is [17 17], that means it's not a single element, it 's a 17x17 matrix, you can't assign it to P(3,3)
Fred John
Fred John 2014년 11월 23일
I believe it said [17 17] as I was just experimenting around with something; irrelevant. I haven't assigned p to be anything yet, which is what I'm trying to do. I want to call the function to the element to the matrix P.
Azzi Abdelmalek
Azzi Abdelmalek 2014년 11월 23일
편집: Azzi Abdelmalek 2014년 11월 23일
To make things clears, post an example
function [p] = func1(a,b,c)
p = a*b + (1-a)*c;
end
Above is function I have written, then
P = zeros(3,3);
P(1,1) = 1;
P(2,2) = 1;
P(3,3) = p;
Then above is command to give matrix of zeros, with diagonal 1, 1 and p (which varies according to a,b and c)
You have to call p
a=1;
b=1;
c=1;
p=func1(a,b,c)
P(1,1) = 1;
P(2,2) = 1;
P(3,3) = p;
Fred John
Fred John 2014년 11월 23일
Right, in your code you have called p with your values of a, b and c (1, 1, 1). But my values of a, b and c vary according to some external data that I have. So how can I call p with that data?
What are these external data? We can't guess!
Fred John
Fred John 2014년 11월 23일
The data is just 20 different values for a, b and c. I have the data but I don't know which format it should be in. Excel etc.
Btw I may seem like a newbie to Matlab, but I have used Matlab about 18 months ago, so apologies if its not clear!
That means you are not asking the right question. Your problem is how to read those data. use xlsread function to read your data
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일

0 개 추천

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

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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2014년 11월 23일

댓글:

2014년 11월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by