Why doesn't this program work?

1 vue (au cours des 30 derniers jours)
John Kirk
John Kirk le 28 Sep 2012
% Weird1:
% Demonstrates some bizaar behavior of MatLAB
% Apparently scalar multiplication flips a vector from row to column.
% Or it is against the rules to put a variable on both sides of the
% equal sign; for example X(i) = 2*X(i)
% John Kirk email John.Kirk@Raymondcorp.com
clear all; clc; close all;
rpm = zeros(1000,1); % Now create all the data
for ii=1:1000;
if mod(ii,2) == 0; % is it even?
rpm(ii)=ii;
else
rpm(ii)=0;
end
end %rpm should now be vector with every other element zero
%Remove zero points for ii=2:(n-1);
count(1)=1;
count(1000)=1000;
for ii=2:(1000-1);
count(ii) = count(ii)+1;
if rpm(ii)==0;% if rpm(ii) is zero then
rpm(ii)=(rpm(ii-1)+rpm(ii+1))/2 % use average of non-zero points
else
% if it is not zero then do nothing to rpm(ii)
end
% shouldn't this statement be executed for every loop?
rpm(ii)= rpm(ii)*2;% multiply every element in rpm by 2
end% of for loop
plot(count,rpm)
% END of program *******************************************
% We have several theories for why this program does not work.
% One is that referring to rpm(ii) on both sides of the '='
% sign confuses MatLAB. The other, and almost certainly true,
% idea is we do not understand how MatLAB handles vectors and
% matrix's. We imagine that we are simply multiplying a
% scalar time each element in a vector when in fact we are
% doing matrix math and flipping a row vector into a column
% vector.
  1 commentaire
Jan
Jan le 28 Sep 2012
I cannot see any bizarre behavior. X(i)=X(i)*2 does not change the shape of X.

Connectez-vous pour commenter.

Réponse acceptée

Matt Fig
Matt Fig le 28 Sep 2012
Modifié(e) : Matt Fig le 28 Sep 2012
I don't see the problem?? The code appears to behave exactly how it is programmed. The one thing I think stands out is the comment on this line:
rpm(ii)= rpm(ii)*2% multiply every element in rpm by 2
This line does not do what the comment says it does. If you want to multiply every element of rpm by 2, you do:
rpm = rpm*2; % It's called 'scalar expansion' - a good thing!
What the first bit of code does is multiply only the element at ii by 2. Thus it the comment should read:
% multiply element ii by 2
Is that your concern, or is there something else? When you say the program "doesn't work" I think you mean it "doesn't do what I expect it to do." But there is a big difference between those statements! So what do you expect it to do? (BTW, I think nothing is substantially changed by replacing each instance of 1000 by 10 so that we can keep better track of things....)

Plus de réponses (1)

owr
owr le 28 Sep 2012
The only row vector vs. column vector issues I see here (I ran the code) is between "rpm" and "count".
rpm is defined to be a column vector with this:
rpm = zeros(1000,1);
and remains a column vector throughout all your computations.
count in indirectly defined to be a row vector - indirectly because you never initialize it with anything but this:
count(1)=1;
count(1000)=1000;
When you introduce a variable like this MATLAB automatically creates it as a row vector with exactly as many elements as it needs. The first line creates a 1 x 1 row vector, the second stretches it out to a 1 x 1000 element row vector with zeros in positions 2,...,999
  1 commentaire
Matt Fig
Matt Fig le 28 Sep 2012
Modifié(e) : Matt Fig le 28 Sep 2012
Yet this is not a problem or a conflict in any way. In the code, count and rpm do not interact... If count were defined as
count = zeros(1000,1);
before the assigning of the values at 1 and 1000, nothing else about the code would be different whatsoever.

Connectez-vous pour commenter.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by