필터 지우기
필터 지우기

Error using .* Matrix dimensions must agree.

조회 수: 3 (최근 30일)
jarvan
jarvan 2014년 11월 5일
댓글: Adam 2014년 11월 5일
Hi,
I got a trouble when I tried to load the date file and update the new balance.
Here is my atm.mat file
password = 'ucsdmate'
balance = num1str(9.5405*100,'%10.2f%')
recentdate= datenum(now)
rate = num2str(0.02*100,'%5.2f%%')
And here is what my problem.
firstdate = datenum(2012,11,04);
days = abs(recentdate - firstdate);
newbalance = balance.*((1+rate).^days);
disp ('Your balance from %d is %10.2f',firstdate,newbalance)
It said Error using .* Matrix dimensions must agree. I don't know where the problem is, I have already used .* but it still didnt work for me

채택된 답변

the cyclist
the cyclist 2014년 11월 5일
It seems like you have converted numeric values to strings (for example rate and balance), but then you are trying to use those strings in numeric operations.
Why are you using num2str?
  댓글 수: 3
jarvan
jarvan 2014년 11월 5일
for the balance, i have to keep it with format 10.2f. And for the interest rate, I have to keep it in %, like 2%
I searched on internet and num2str satisfied my requirement. should I change to like newbalance = num2str(balance.*((1+rate).^days)) or something?
Adam
Adam 2014년 11월 5일
You should keep all numbers as numbers and convert to strings only where needed. If you have to do subsequent maths on something converting it to and from string is just not efficient.
Either convert to strings only in disp or do it only at the end of the maths or even keep another variable representing the string though unless you want to use OOP and dependent properties I would not recommend the last solution.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by