??? Subscripted assignment dimension mismatch.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Fred John
am 23 Nov. 2014
Kommentiert: Fred John
am 23 Nov. 2014
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 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 23 Nov. 2014
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 Kommentare
Geoff Hayes
am 23 Nov. 2014
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.
Weitere Antworten (1)
Azzi Abdelmalek
am 23 Nov. 2014
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 Kommentar
Azzi Abdelmalek
am 23 Nov. 2014
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
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!