Basic Vector Operations

33 ビュー (過去 30 日間)
Prince
Prince 2011 年 5 月 23 日
コメント済み: westley 2025 年 7 月 30 日
Hello, I am currently familiarizing myself with MATLAB for my research. I was asked to create a program that reads in two vectors from the user and then graph them. I am currently having difficulty correctly performing the the addition and multiplication of these vectors. I am new to this website and MATLAB as a whole. I was hoping someone could assist me in any way.
Thank you, first and foremost for your support. I have spent the last couple of days researching operation techniques and I have come up with the following code for each different function (all of function data may not be included)
for addition:
a = get(handles.textvector1,'String');
b = get(handles.textvector2,'String');
%if length is the same
if length(a) == length(b)
%add vectors
c = a + b;
%set text to result
set(handles.textvectorresult,'String',c);
%plot result
plot(c)
% or display error message
else
errordlg('Please enter vectors of same size','Size mismatch error','modal')
end
and for multiplication:
a = get(handles.textvector1,'String');
b = get(handles.textvector2,'String');
%if length is the same
if length(a) == length(b)
%add vectors
c = a .* b;
%set text to result
set(handles.textvectorresult,'String',c);
%plot result
plot(c)
% or display error message
else
errordlg('Please enter vectors of same size','Size mismatch error','modal')
end
I end up getting unusually high numbers
Thanks, Prince Njoku
  1 件のコメント
Oleg Komarov
Oleg Komarov 2011 年 5 月 23 日
http://www.mathworks.com/matlabcentral/answers/6200-tutorial-how-to-ask-a-question-on-answers-and-get-a-fast-answer

サインインしてコメントする。

採用された回答

Oleg Komarov
Oleg Komarov 2011 年 5 月 23 日
  • Multiplication (note the dimensions):
(1:10) * (21:30).' = scalar % 1 by 10 times 10 by 1 - inner/dot product
(1:10).' * (21:30) = matrix % 10 by 1 times 1 by 10 - outer product
(1:10) .* (21:30) = vector % elementwise multiplication
.
  • Summation (again note the dimensions)
(1:10) + 10 or rand(2) + 10 % vector/matrix plus scalar
(1:10) + (11:20) % same dim vector addition
rand(2) + ones(2) % same dim matrix addition
EDIT
You're retrieving text and doing operations on strings not numbers. To see what I mean try this:
'65' .* '33'
65 * 33
To convert strings to numbers a wrapper is:
str2double('65')
or manually for positive numbers:
tmp = '65' - '0';
nTmp = numel(tmp);
out = tmp*(10.^(nTmp-1:-1:0)).';
  7 件のコメント
Prince
Prince 2011 年 5 月 23 日
thanks so much!
westley
westley 2025 年 7 月 30 日
thank you so much man!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by