Azzera filtri
Azzera filtri

Size of the left side different from the right side

67 visualizzazioni (ultimi 30 giorni)
Inbal
Inbal il 6 Ago 2024 alle 23:01
Modificato: Walter Roberson il 7 Ago 2024 alle 17:57
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 Commenti
Walter Roberson
Walter Roberson il 7 Ago 2024 alle 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.
Inbal
Inbal il 7 Ago 2024 alle 7:05
Modificato: Inbal il 7 Ago 2024 alle 7:45
Thanks a lot!
"if isempty(SPAnsVector{1,i})==0" isn't working, do you have another idea how to put only non-blank cells (not "NaN") in a new vecor?

Accedi per commentare.

Risposte (3)

Walter Roberson
Walter Roberson il 7 Ago 2024 alle 0:14
Modificato: Walter Roberson il 7 Ago 2024 alle 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
dpb il 7 Ago 2024 alle 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 = 1x7
1 2 3 5 6 7 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
X(7)=[] % try another way...
X = 1x6
1 2 3 5 6 7
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
X = 1x8 cell array
{[1]} {[2]} {[3]} {0x0 double} {[5]} {[6]} {[7]} {[8]}
isempty(X) % is the array empty (obviously, no...)
ans = logical
0
arrayfun(@isempty,X) % are the elements of the cell array empty??? Again, no!!!
ans = 1x8 logical array
0 0 0 0 0 0 0 0
cellfun(@isempty,X) % is the _content_ of any of the cell elements empty??? Yes...
ans = 1x8 logical array
0 0 0 1 0 0 0 0
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...

Umar
Umar il 7 Ago 2024 alle 0:40
Modificato: Walter Roberson il 7 Ago 2024 alle 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 Commenti
dpb
dpb il 7 Ago 2024 alle 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.
Inbal
Inbal il 7 Ago 2024 alle 15:30
@dpb thanks,
I found a solution that works fine.
Thanks a lot for all your help @Walter Roberson @Umar@dpb
Inbal

Accedi per commentare.

Categorie

Scopri di più su Data Import from MATLAB in Help Center e File Exchange

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by