multiplying/dividing matrices that contain string variables and NaN

Dear all
I have
I have
A= [
29 1;
27 4;
31 0;
28 3;
25 5;
]
and
B={'id1' 'id2' 'id3' 'id4'
[0.4607] [0.4400] [ 0.9167] [0.8701]
[0.4400] [0.4432] [ NaN] [ 0.8808]
[0.4432] [ 0.4419] [ 0.8808] [ 0.8809]
[0.4419] [NaN] [ 0.8809] [ 0.9156]
[0.4607] [0.4547] [ 0.9156] [ 0.9039]
}
and
A1=A(:,1)
And I want to calculate
sum(A.*B(:,1:2),2)/A1
sum(A.*B(:,3:4),2)/A1
in one step because my B contains 120 columns and I want to multiply A with every 2 columns of B that is B(:,1:2) B(:,3:4) B(:,5:6)
S0, I am looking something like
sum(A.*[B(:,1:2) B(:,3:4) B(:,5:6) ],2)/A1
thanks

2 件のコメント

Sabbas
Sabbas 2012 年 7 月 29 日
Any help?
thanks
Oleg Komarov
Oleg Komarov 2012 年 7 月 29 日
As a general approach I would suggest to avoid storing double matrices in cell arrays, makes things unnecessarily complicated.

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

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 7 月 29 日
編集済み: Azzi Abdelmalek 2012 年 7 月 29 日

0 投票

a=B(2:end,:)
ind=cellfun(@(x) ~isstr(x),a);b=a;
b(find(ind==0))=num2cell(0)
C=cell2mat(b);
n=size(b,2);D=[];
for k=1:2:n-1
D=[D A.*C(:,k:k+1)]
end
Result=sum(D,2)./A(:,1)

4 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 7 月 29 日
sorry there is an error, ';' instead of ':' i will fix it
Sabbas
Sabbas 2012 年 7 月 29 日
is it ok now? thanks
Sabbas
Sabbas 2012 年 7 月 29 日
編集済み: Sabbas 2012 年 7 月 29 日
could someone help me please? I do not know how to do it
thank you very much
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 7 月 29 日
did you try my code?

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

その他の回答 (1 件)

Oleg Komarov
Oleg Komarov 2012 年 7 月 29 日

1 投票

Using the same approach from my answer to your previous post:
% First extract only the data
Bdata = cell2mat(B(2:end,:));
bsxfun(@rdivide,squeeze(nansum(bsxfun(@times, reshape(Bdata,5,2,[]),A),2)),A(:,1))

2 件のコメント

Sabbas
Sabbas 2012 年 7 月 29 日
When I apply the above code I get the following error message
bsxfun(@times, reshape(out2,ll,2,[]),AS)
Error using bsxfun
Non-singleton dimensions of the two input arrays must match each other.
Am I doing something wrong?
thanks
Oleg Komarov
Oleg Komarov 2012 年 7 月 30 日
As should have at least one dimension the same length as the reshaped out2.

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

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by