Need help processing this

So i have e.g data < 3x3 int 16 >
4 5 3
5 4 1
2 1 2
i wanted to got
78 60 23
and the process is like :
Column 1->(4*4*1)+(5*5*2)+(2*2*3) then Column 2->(5*5*1)+(4*4*2)+(1*1*3) then Column 3->(3*3*1+1*1*2+2*2*3)
i tried this :
[g,h]=size(data);
m = int16(1:g);
t=sum(data(:,m).*data(:,m).*m);
but i have matrix dimension problem, How can i solve,achieve this?

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 3월 12일

1 개 추천

out = cast((1:size(A,1))*double(A.*A),class(A));

댓글 수: 4

I Made
I Made 2013년 3월 12일
wow great it's working, but i don't know if it's right or not yet because i got very large number and i think int 16 cannot handle it as it always display 32767, am i correct?
Cedric
Cedric 2013년 3월 12일
편집: Cedric 2013년 3월 12일
If you are not interested in the output to be the same type as A, you can do:
out = (1:size(A,1)) * double(A).^2 ;
so you are not limited by int16 upper limit.
I Made
I Made 2013년 3월 12일
편집: I Made 2013년 3월 12일
Works like magic.. thanks andrei tough i don't understand yet the principle of the precision using double or using class like you mention on your first answer.
Cedric
Cedric 2013년 3월 13일
편집: Cedric 2013년 3월 13일
Andrei type-casts A.*A to double so it can be multiplied by the vector (1:size(..)) avoiding the error that you got when you tried my solution (that I wrote without paying enough attention to the fact that A was int16). He then type-casts back to the original type afterwards (which is the type/class of A).
In my comment above, I type-cast A to double before squaring it, so the square is not truncated by int16 limits. I don't type-cast back to the type of A, because I don't think that you need that (and there would be the truncation issue as the matrix multiplication performs a weighted sum of squares with weights >=1).

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

추가 답변 (0개)

카테고리

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

태그

질문:

2013년 3월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by