??? 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?
Geoff Hayes
Geoff Hayes 2014 年 11 月 23 日
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.
Geoff Hayes
Geoff Hayes 2014 年 11 月 23 日
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?
Fred John
Fred John 2014 年 11 月 23 日
size(p)
ans =
17 17
no I tried that with my own function, and it did not work either. size(p) gives above.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 11 月 23 日
If the size of p is [17 17] how can you write?
P(3,3) = p
Fred John
Fred John 2014 年 11 月 23 日
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?
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 11 月 23 日
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
Fred John
Fred John 2014 年 11 月 23 日
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)
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 11 月 23 日
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?
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 11 月 23 日
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!
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 11 月 23 日
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 件のコメント

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

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

カテゴリ

タグ

質問済み:

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