Summing Elementwise Values in a Vector

3 ビュー (過去 30 日間)
Frank Lehmann
Frank Lehmann 2020 年 9 月 14 日
回答済み: Mahesh Taparia 2020 年 9 月 16 日
clc
if vs=="AC"
c=0.8;
iv=acvi;
else
c=1;
iv=dcvi;
end
for k =1:numel(lo);
lva=lo(k)./c;
lvah=lva.*t;
ll=lo(k)./iv;
end
slo=sum(lo);
I have a sample script which i input to the above as a "function" ('AC',[25 45 65 78 120 65],8, 415,24) ie a string, vector and doubles now when i sum the input vector there is no drama for the initial vector however when i sum the other vectors, (lva,lvah,ll) i get the correct values after elementwise manipulation but cannot sum them i only the output of the last known value. Any ideas? I'm sure the solution is quite simple but its racking my brain. :(
Thanks,
Frank
  2 件のコメント
KSSV
KSSV 2020 年 9 月 14 日
Replace
vs=="AC"
with
strcmp(vs,'AC')
Frank Lehmann
Frank Lehmann 2020 年 9 月 14 日
KSSV your comment is legitimate but not the answer i was looking for i want to sum the elementwise vectors?

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

回答 (1 件)

Mahesh Taparia
Mahesh Taparia 2020 年 9 月 16 日
Hi
You can refer the below code which can be used to find the sum of the elements which you are doing. Hope it will help!
function [slva, slvah, sll, slo] = compute(vs,lo, acvi, dcvi, t)
if strcmp(vs, 'AC')
c = 0.8;
iv = acvi;
else
c = 1;
iv = dcvi;
end
lva = lo/c;
lvah = lva * t;
ll = lo/iv;
slo = sum(lo);
slva = sum(lva);
slvah = sum(lvah);
sll = sum(ll);
end

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by