フィルターのクリア

how can I multiply a vector by scalar?

24 ビュー (過去 30 日間)
Parham Babakhani Dehkordi
Parham Babakhani Dehkordi 2016 年 5 月 19 日
コメント済み: the cyclist 2016 年 5 月 19 日
Hi, I have a very simple case. I do not know why there is an error which says, (Undefined operator '*' for input arguments of type 'cell').C1 is a [1*44998] and k=[1*300] vectors. my purpose is to calculate the vector of sss in which a scalar value of 2.7 is multiplied by vector C (which has a size [1*300]. any help would be appreciated.
k=find(c1==1);
time1=t(k);
i=diff(k);
j=find(i>1);
j=[0,j];
n=length(j);
for m=1:n-1;
I{m}=k(j(m)+1:j(m+1))
time{m}=t(I{m})
initial_time{m}=time{m}(1)
final_time{m}=time{m}(end)
end
I{m+1}=k(j(end)+1:end)
time{m+1}=t(I{m+1})
initial_time{m+1}=time{m+1}(1)
final_time{m+1}=time{m+1}(end)
C = cellfun(@minus,final_time,initial_time,'UniformOutput',false)
sss=C*2.7;

採用された回答

the cyclist
the cyclist 2016 年 5 月 19 日
編集済み: the cyclist 2016 年 5 月 19 日
A cell array is a special type of "container", which you cannot do all operations on. You need to use a numeric array for numerical operations. Depending on the contents of C, you might just be able to do
C_num = cell2mat(C)
and carry out your operations on that.
  2 件のコメント
Parham Babakhani Dehkordi
Parham Babakhani Dehkordi 2016 年 5 月 19 日
well-done
the cyclist
the cyclist 2016 年 5 月 19 日
You might want to back-track and see if C should have been a numerical array all along, rather than being stored in a cell array.

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

その他の回答 (2 件)

Jos (10584)
Jos (10584) 2016 年 5 月 19 日
編集済み: Jos (10584) 2016 年 5 月 19 日
C is a cell array. I think you want the content of a cell in C to be multiplied by 2.7. You can do this using cellfun
C = {[10 7 4], 100, 1:5} % a cellarray
C2 = cellfun(@(x) 2.7*x, C, 'un', 0)

dpb
dpb 2016 年 5 月 19 日
You get the error because C isn't a vector but a cell containing a vector. You can work around it by
C=C{:}*2.7; % result is double vector
or
C={C{:}*2.7}; % result is cast back to a cell
W/o a way to run your code snippet easily I'm not positive but it's likely you could use logical addressing and avoid creating the cell array to begin with...

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by