From: Roger Stafford on
"Chen " <neversaynever(a)never.org> wrote in message <i422ov$3u9$1(a)fred.mathworks.com>...
> Hi Roger,
>
> Here is what I got. Maybe it's a little silly but the results are what i wanted. Each element in f only shows once in the new 9 X 9 matrix. Thank u agian for alwasy giving great advice to me.
>
> -Chen
>
> a = [1 2 3; 16 13 10;4 6 8];
> b = [0.5 0.6 0.7; 1 1.2 1.4; 2 2.4 2.8];
> f = a.*b;
> a = a';
> b = b';
> [m n] = size(a);
> A = reshape(a,1,m*n);
> A = sort(A);
> B = reshape(b,1,m*n);
> B = sort(B);
> ff = zeros(m*n);
> a = a';
> b = b';
> for i = 1:length(A);
> for j = 1:length(B);
> [index_a_row index_a_col] = find(a == A(i));
> [index_b_row index_b_col] = find(b == B(j));
> if ((index_a_row == index_b_row) && (index_a_col == index_b_col))
> ff(i,j) = A(i)*B(j);
> end
> end
> end
> ff = ff';
- - - - - - - - - - - -
Was there something wrong with the solution I sent to you? I tested it out on your example and the results matched your desired f_new. The code you have written here takes a lot more operations and probably a lot more cpu time.

Roger Stafford
From: Chen on

> Was there something wrong with the solution I sent to you? I tested it out on your example and the results matched your desired f_new. The code you have written here takes a lot more operations and probably a lot more cpu time.
>
> Roger Stafford


Hi Roger,

There are only one non-zero element in each column from my testing code and there are three non-zeros element in each column from yours. But I am thing to make use of yours to reduce the running time. I just keep using loop which is time-consuming...

-Chen
From: Roger Stafford on
"Chen " <neversaynever(a)never.org> wrote in message <i43b49$c8l$1(a)fred.mathworks.com>...
> Hi Roger,
>
> There are only one non-zero element in each column from my testing code and there are three non-zeros element in each column from yours. But I am thing to make use of yours to reduce the running time. I just keep using loop which is time-consuming...
>
> -Chen
- - - - - - - - - -
You are right, Chen. My apologies. Between the time I tested the code and the time I sent it to you, I made an error in copying it. I thereby failed to turn x and y into column vectors when they were to be sorted. It should have read this way:

x = [1 2 3; 16 13 10; 4 6 8];
y = [0.5 0.6 0.7; 1 1.2 1.4; 2 2.4 2.8];
ff = diag(x(:).*y(:));
[t,p] = sort(x(:));
[t,q] = sort(y(:));
f_new = ff(q,p);

Roger Stafford