Unable to perform assignment because the left and right sides have a different number of elements.

i=3;
clmax= zeros(i,1);
for j = 1: 1: i
clmax(j) = del (j).clmax;
end

5 Comments

When I run this code, I just get an error because you have not defined del for us.
i=3;
clmax= zeros(i,1);
for j = 1: 1: i
clmax(j) = del (j).clmax;
end
Unrecognized function or variable 'del'.
Can you share the full code that gives the error?
function [] = func(del)
i=3;
clmax= zeros(i,1);
for j = 1: 1: i
clmax(j) = del (j).clmax;
end
We still do not know, what the comtents of del(j).clmax is. If the contents of the fields are not scalars, the assignment must fail.
The size of del does not matter, but the size of its fields clmax.

Sign in to comment.

Answers (1)

The error means, that the contents of the fields clmax are not scalar. Check this:
clmax = {del.clmax};
s = cellfun('prodofsize', clmax)
Storing the data in a cell array is the best way, if the fields have different sizes. If call have the same dimension, you can concatenate them to a matrix or a n-dim array:
clmax = cat(1, del.clmax) % Or 2, 3, or what ever
You see, that neither for a cell array nor for a numerical array a loop is required.

Categories

Asked:

MG
on 29 Jun 2021

Edited:

Jan
on 30 Jun 2021

Community Treasure Hunt

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

Start Hunting!