Error: Undefined function 'mtimes' for input arguments of type 'cell' -2

1 visualizzazione (ultimi 30 giorni)
Hello,
I am getting this error "Undefined function 'mtimes' for input arguments of type 'cell'. when I am trying to run this code. I read the previous posts as well on the forum, but what changes I have to make in my code.
here is what I am doing
L = 64; % Length of signal
for io = 1:1:size(NumericalFourierForce,1)
for jo =1:1:2
P1{io,jo} = NumericalFourierForce{io,1}{jo,1}(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1); % this line causes the error
end
end
I have attached the file with the question. It is a 5x1 cell array with each cell having 2x1 cell and then they have two times 1x64 elements.
What changes should I make to my code.

Risposta accettata

Walter Roberson
Walter Roberson il 12 Lug 2020
P1{io,jo} = NumericalFourierForce{io,1}{jo,1}(1:L/2+1);
The assignment on that line would fail if P1 is an existing variable that is not a cell array or table or string array (or possibly some other object types.) In particular it would fail if P1 is an existing numeric array. The assignment would not fail of P1 does not already exist, in which case P1 would be constructed as a cell array. In each case -- cell array, table, string array -- the line would fail if P1 exists already and is not a "container" object.
P1(2:end-1) = 2*P1(2:end-1); % this line causes the error
When you use () indexing to access a container object, the result is a container object. If we assume that P1 is a cell array of numeric arrays, then when you index using () you just get a smaller cell array of numeric arrays. But you cannot do arithmetic on cell arrays, only on their contents.
You could replace
P1(2:end-1) = 2*P1(2:end-1); % this line causes the error
with
P1(2:end-1) = cellfun(@(C) 2*C, P1(2:end-1), 'uniform', 0);
but I recommend that you reconsider that! You are using linear indexing into a 2D array that appears to be dynamically growing. After you create P1{2,1} then P1 would be 2 x 2 with P1{2,2} not yet initialized, and P1(2:end-1) would be P1(2,1), P1(1,2) so you would double those. Then you create P1{2,2} and P1(2:end-1) would again be P1(2,1) and P1(1,2) and you would again double those.
The doubling of multiple elements should not be in the for loop.
Maybe something like,
numrow = size(NumericalFourierForce,1);
for io = 1 : numrow
for jo =1:1:2
temp = NumericalFourierForce{io,1}{jo,1}(1:L/2+1);
if io ~= 1 && io ~= numrow
temp = temp * 2;
end
P1{io,jo} = temp;
end
end
this does not do exactly the same thing as what you had programmed, but it seems a bit more plausible than what you were doing.
However, if this is what you wanted to program, then...
numrow = size(NumericalFourierForce,1);
for io = 1 : numrow
for jo =1:1:2
P1{io,jo} = NumericalFourierForce{io,1}{jo,1}(1:L/2+1);
end
end
P1(2:end,:) = cellfun(@(C) 2*C, P1(2:end,:), 'uniform', 0);

Più risposte (0)

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by