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일

1 개 추천

  • 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 again. When I convert the strings to a double I recieve one large number. what I am attempting to do is add, subtract, multiply and divide each corresponding element of the two vectors. is there a way to convert each element to it's own integer?
thanks
Oleg Komarov
Oleg Komarov 2011년 5월 23일
Can you please post an example input string and what you're trying to accomplish.
Does tmp = '65' - '0'; satisfy your request (to separate a string in numeric integers)?
Prince
Prince 2011년 5월 23일
I appreciate your patience.the input is constructed from the user's choice in my code. I prompt the user for a list of a line of numbers with no specific length. then I prompt them for another line with the same length.
Example:
vector 1 = [1 2 3 4 5]
vector 2 = [2 4 5 6 2]
*vectors should be same length in order to operate
could you please explain tmp = '65'-'0' a little further please
thanks
Prince
Prince 2011년 5월 23일
*missing part*
the result should look something like this if you add:
vector result = [3 6 8 10 7]
thanks
Oleg Komarov
Oleg Komarov 2011년 5월 23일
vector1 + vector2 gives exactly result if they are numeric.
If they are strings
vec1 = '12345';
vec2 = '24562';
Then you need to convert into separate integers.
vec1 -'0' does exactly that.
A string number (ex: '1') is decoded with it's corresponding ASCII numeric value, thus '1' -->double('1') = 49.
'1'-'0' = 49 - 48 = 1 and the result is displayed as double because of the operator "minus".
'123' - '0' operates on all elements of the string array and returns a double vector [1 2 3] and not the scalar 123 directly.
Prince
Prince 2011년 5월 23일
thanks so much!
westley
westley 2025년 7월 30일
thank you so much man!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2011년 5월 23일

댓글:

2025년 7월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by