Size of the left side different from the right side
67 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi everyone, I'm facing a problem with assignment because of different sizes.
I'm pretty sure the sizes are the same, its look like MATLAB refer to the input size instead of the output (in the relevant function).
the error massege: "Unable to perform assignment because the size of the left side is 1-by-6 and the size of the right side
is 1-by-108".
thanks a lot!
the problematic line:
[PersonalSPQmain(i,:),PersonalSPAnsmain(i,:)] = PersonalSPQforResponder(SParraydata(i,:));
My code -main function:
SParraydata=table2array(SPdata);
personalSPdata=zeros(51444,2);
PersonalSPQmain=zeros(8574,6);
PersonalSPAnsmain=zeros(8574,6);
j=1;
for i=1:8574
[PersonalSPQmain(i,:),PersonalSPAnsmain(i,:)] = PersonalSPQforResponder(SParraydata(i,:));
personalSPdata(j:j+5,2) = sixSPAnsLines(PersonalSPQmain(i,:),PersonalSPAnsmain(i,:));
j=j+5;
end
The relevent function:
function [PersonalSPQ,PersonalSPAns] = PersonalSPQforResponder(SPAnsVector)
PersonalSPQ=zeros(1,6);
PersonalSPAns=zeros(1,6);
j=1;
for i=1:108
if isempty(SPAnsVector(1,i))==0
PersonalSPQ(1,j)=i;
PersonalSPAns(1,j)=SPAnsVector(1,i);
j=j+1;
end
end
end
2 Kommentare
Walter Roberson
am 7 Aug. 2024 um 0:12
if isempty(SPAnsVector(1,i))==0
In MATLAB, the isempty() test there will (almost) always turn out false.
(In order for it to be true, you would have to be using a class object of a class that overrides subsref() or else has to derive from matlab.mixin.indexing.RedefinesParen and provide a parenReference method)
With the exception of what is described above, () indexing in MATLAB might fail with index out of range, but otherwise it returns one or more elements of the same datatype as what is being indexed. Well, I guess it could return zero elements if i is empty or is false ... but i is numeric scalar, and indexing at (1,i) is always going to return a scalar object of the same datatype as is being indexed, and scalars are never empty.
In order for isempty() to apply, you need to be using dot indexing or {} indexing, such as
if isempty(SPAnsVector{1,i})==0
However, {} indexing fails for numeric arrays such as SPAnsVector.
Antworten (3)
Walter Roberson
am 7 Aug. 2024 um 0:14
Bearbeitet: Walter Roberson
am 7 Aug. 2024 um 0:15
for i=1:108
if isempty(SPAnsVector(1,i))==0
PersonalSPQ(1,j)=i;
PersonalSPAns(1,j)=SPAnsVector(1,i);
because isempty() will always be false in that context, you will be setting up to PersonalSPAns(1,108), which is why your return vector is size 1 x 108.
dpb
am 7 Aug. 2024 um 0:22
function [PersonalSPQ,PersonalSPAns] = PersonalSPQforResponder(SPAnsVector)
PersonalSPQ=zeros(1,6);
PersonalSPAns=zeros(1,6);
j=1;
for i=1:108
if isempty(SPAnsVector(1,i))==0
PersonalSPQ(1,j)=i;
PersonalSPAns(1,j)=SPAnsVector(1,i);
j=j+1;
end
end
end
The above will always leave size(PersonalSPQ) and size(PersonalSPAns) as 1x108 vectors because it is not possible for an individual element in an ordinary array to be empty.
Illustrations:
X=[1:3 [] 5:8] % try to create an array with a "hole" in it...
X(7)=[] % try another way...
You see that the first case does not create an empty element in the vector, it just returns the nonempty elements ignoring the empty location. So, the vector has only 7 elements, not 8.
Alternatively, trying to create the "hole" by setting a specific element to the empty quantity simply deletes that element from the vector, now leaving it with only six elements.
The only way to have such a case would be by using a cell array in which the array itself is not empty, but it can contain a cell that is empty...
X=[num2cell([1:3]) {[]} num2cell([5:8])] % a cell array with an empty cell
isempty(X) % is the array empty (obviously, no...)
arrayfun(@isempty,X) % are the elements of the cell array empty??? Again, no!!!
cellfun(@isempty,X) % is the _content_ of any of the cell elements empty??? Yes...
You didn't provide the input data to look at; maybe there's supposed to be a missing-value indicator or something; we can't tell...but, MATLAB is not lying to you; it's doing what you're asking it to do. That obviously is not what you intended...
0 Kommentare
Umar
am 7 Aug. 2024 um 0:40
Bearbeitet: Walter Roberson
am 7 Aug. 2024 um 0:50
Hi @Inbal,
Both @dpb and @Wakter Robertson have provided excellent examples related to your problem, you have to check the dimensions of your variables or reshape them accordingly to make the assignment compatible. However, I debugged your code and updated it. Here is example modified code,
function [PersonalSPQ, PersonalSPAns] =
PersonalSPQforResponder(SPAnsVector)
PersonalSPQ = zeros(1, 6);
PersonalSPAns = zeros(1, 6);
j = 1;
for i = 1:numel(SPAnsVector)
if ~isempty(SPAnsVector(i))
PersonalSPQ(j) = i; % Update index directly
PersonalSPAns(j) = SPAnsVector(i); % Update index directly
j = j + 1;
end
end
end
% Define a generic table 'SPdata' for demonstration purposes
SPdata = table(rand(108,1), 'VariableNames', {'Data'});
SParraydata = table2array(SPdata);
personalSPdata = zeros(51444, 2);
PersonalSPQmain = zeros(8574, 6);
PersonalSPAnsmain = zeros(8574, 6);
j = 1;
% Define the local function outside the loop
function output = sixSPAnsLines(PersonalSPQ, PersonalSPAns)
% Local function logic
output = PersonalSPQ + PersonalSPAns; % Placeholder logic
end
for i = 1:min(8574, size(SParraydata, 1)) % Ensure loop bounds within array size
[PersonalSPQ, PersonalSPAns] =
PersonalSPQforResponder(SParraydata(i,:));
% Update the assignment targets with the correct sizes
PersonalSPQmain(i,:) = PersonalSPQ; % Ensure sizes match
PersonalSPAnsmain(i,:) = PersonalSPAns; % Ensure sizes match
personalSPdata(j:j+5, 2) = sixSPAnsLines(PersonalSPQ, PersonalSPAns);
j = j + 6; % Increment by 6 since we are updating 6 rows
end
Please let me know if this helps resolve your problem.
6 Kommentare
dpb
am 7 Aug. 2024 um 13:49
@Inbal, the code with the table was not from Walter, but from @Umar; as he noted below, he was just trying to write something that would run; it more than likely doesn't solve your real problem which you still have not described sufficiently that we can tell what it is that is your actual end objective (other than just making the error go away).
We need to have a sample dataset and what is supposed to be the indication of a missing value within it and the logic that would indicate the expected size of the result array would be six elements long--it doesn't seem likely that you would know that a priori unless there is some specific reason for it in the definition of the problem--but we don't know what the underlying problem is...
More than likely, if you would describe the actual problem trying to be solved the answer could be coded completely using <logical indexing> in which case the whole issue of sizes can become transparent by use of MATLAB's automagic allocation on assignment.
BUT, to do that we need what we don't have -- the actual code requirements and the sample input data.
Siehe auch
Kategorien
Mehr zu Data Import from MATLAB 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!