Azzera filtri
Azzera filtri

Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

for loop indexing in temporary arrary problem in MATLAB.

1 visualizzazione (ultimi 30 giorni)
hello_world
hello_world il 18 Mag 2016
Chiuso: Geoff Hayes il 18 Mag 2016
Hello Friends,
I have the following code:
P = [1, 0, 3];
Q = [0, 4, 5];
AB = [P + Q]; % A vector
CD = [P - Q]; % A vector
EF = [P ./ Q]; % A vector
M = {'AB', 'CD', 'EF'};
for i = 1:length(M)
P1 = P(M{i}~=0); %It removes those elements of P which correspond to 0 entries in M.
t = M{i}; %Create a temporary variable.
M = t(M{i}~=0); %It removes 0 entries from M.
P = P1(~isnan(M(i))); %It removes those elements of P which correspond to NaN entries in M.
M = t(~isnan(M(i))); %It removes NaN entries from M.
if strcmp(M, 'AB')
f = f(P,M);
elseif strcmp(M, 'CD')
f = g(Q,M);
elseif strcmp(M, 'EF')
f = h(R,M);
end
end
This code is not computing P1, t, M, P values properly. For example the following line of code takes M{i} = AB for i = 1, but gives totally wrong answer.
I have tried to change {} to () and [], etc., but nothing works. I will appreciate any advice.
P1 = P(M{i}~=0);

Risposte (2)

Geoff Hayes
Geoff Hayes il 18 Mag 2016
Your line of code has me confused
M = t(M{i}~=0); %It removes 0 entries from M.
The comment says that you are removing zero entries from M. But M has been defined to be a cell array of strings as
M = {'AB', 'CD', 'EF'};
What is the intent of the above assignment? Do you really mean for M to be a cell array of strings, or do you mean it to be a cell array of numbers?
M = {AB, CD, EF};
Also, the line of code
CD = [P*Q]
will generate an error since P and Q do not have the compatible dimensions for matrix multiplication.
Use the debugger to step through the code and you will probably get a good idea as to what is going on. Always look at the variables to verify that they are (with respect to dimension, type, etc.) what you expect them to be.
  3 Commenti
Geoff Hayes
Geoff Hayes il 18 Mag 2016
But you will still have the same problem of your M cell array containing strings and not numbers.
M = {'AB', 'CD', 'EF'};
so M{1} is the string 'AB' and not the sum of P and Q. I would just do
M = {AB, CD, EF}
which will allow you to continue, but will cause other problems. The for loop is iterating over each element of M but then you do
M = t(M{i}~=0);
which removes the zero entries from M{i} and not from M as the comment indicates. I suspect that you may want
M{i} = t(M{i}~=0);
The subsequent lines using P and P1 are confusing. And then the code goes back to comparing M to strings.
You have to decide what M is - is it a cell array of strings or a cell array of numbers?

Todd Leonhardt
Todd Leonhardt il 18 Mag 2016
I don't understand how that code could even get far enough to attempt to compute P1, etc. It should error out on the 4th line of code where you attempt to compute:
CD = P * Q;
P and Q are both 1 x 3 matrices, so you can not perform matrix multiplication on them as such. You can do any one of the following:
CD = P' * Q; % Take transpose of P, P', so it is a 3 x 1 matrix and results is a 3x3
CD = P * Q'; % Take transpose of Q, Q', so it is a 3 x 1 matrix and result is a 1x1 scalar
CD = P .* Q; % Perform element-wise multiplication so result is a 1 x 3
  1 Commento
hello_world
hello_world il 18 Mag 2016
Hi Todd,
Thank you for your reply. I have commented above to Geoff as he had the similar questions. Please check my modified question.

Questa domanda è chiusa.

Community Treasure Hunt

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

Start Hunting!

Translated by