From: Chen on
Dear all,

The problem is like this:

Lets say 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];

the data is like this distributed: diff(x(1,:)) = 1; diff(x(2,:)) = 3; diff(x(3,:)) = 2;
diff(y(1,:)) = 0.1; diff(x(2,:)) = 0.2; diff(x(3,:)) = 0.4;

which means that the numbers in each row of x or y are not equally spaced neither in ascending or descending order.

the data I want to put in the new matrix is generated like this:
f = x.*y = [ 0.5000 1.2000 2.1000
16.0000 15.6000 14.0000
8.0000 14.4000 22.4000];

I want to generate a new 9 X 9 matrix for f in such a way that both row and column are in a ascending order w.r.t x or y.

So there are 9 elements in this 9 X 9 matrix and all these 9 elements are obtained by f = x.*y. the rest of this matrix are all zeros.

How can I do this? Thanks !

-Chen




From: Roger Stafford on
"Chen " <neversaynever(a)never.org> wrote in message <i415ha$h7q$1(a)fred.mathworks.com>...
> Dear all,
>
> The problem is like this:
>
> Lets say 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];
>
> the data is like this distributed: diff(x(1,:)) = 1; diff(x(2,:)) = 3; diff(x(3,:)) = 2;
> diff(y(1,:)) = 0.1; diff(x(2,:)) = 0.2; diff(x(3,:)) = 0.4;
>
> which means that the numbers in each row of x or y are not equally spaced neither in ascending or descending order.
>
> the data I want to put in the new matrix is generated like this:
> f = x.*y = [ 0.5000 1.2000 2.1000
> 16.0000 15.6000 14.0000
> 8.0000 14.4000 22.4000];
>
> I want to generate a new 9 X 9 matrix for f in such a way that both row and column are in a ascending order w.r.t x or y.
>
> So there are 9 elements in this 9 X 9 matrix and all these 9 elements are obtained by f = x.*y. the rest of this matrix are all zeros.
>
> How can I do this? Thanks !
>
> -Chen
- - - - - - - -
You have explained the nature of your x and y arrays and how f is obtained from them in a clear manner. From your description it can be seen that an f array of any n x n size can be generated entirely in terms of four n x 1 column vectors.

However, you haven't explained just how x and y are allowed to be altered to make f satisfy your condition: "... generate a new 9 X 9 matrix for f in such a way that both row and column are in a ascending order w.r.t x or y." Furthermore it isn't clear what you mean in that last part about "w.r.t x or y". What do you mean by "with respect to x or y"? Rows and columns are either in ascending order or they aren't.

Can you please make another try at giving a clear explanation of what you are trying to do?

Roger Stafford
From: Chen on
Hi Roger,

Thanks agian for your reply. Sorry I didn't make it clear. I will just give numbers this time to try to make it clear.

Given: 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];
There is a function which is x and y dependent and it's defined as f = x.*y.
So f = [ 0.5 1.2 2.1; 16 15.6 14; 8 14.4 22.4];

I actually want to "reshape" this 3 X 3 matrix to a new 9 X 9 matrix with each element f_new(i,j) w. r .t (new_x(j) ,new_y(i))
new_x = [1, 2, 3, 4, 6, 8, 10, 13, 16]; %% "reshape" vector x in an acsending order.
new_y = [0.5, 0.6, 0.7, 1, 1.2, 1.4, 2, 2.4, 2.8]; %% in acsending order too.

f_new = [0.5 0 0 0 0 0 0 0 0;
0 1.2 0 0 0 0 0 0 0;
0 0 2.1 0 0 0 0 0 0;
0 0 0 0 0 0 0 0 16;
0 0 0 0 0 0 0 15.6 0;
0 0 0 0 0 0 14 0 0 ;
0 0 0 8 0 0 0 0 0 ;
0 0 0 0 14.4 0 0 0 0;
0 0 0 0 0 22.4 0 0 0]
for example: f_new(1,1) = new_x(1)*new_y(1);
f_new(4,9) = new_x(9)*new_y(4);
f_new(8,5) = new_x(5)*new_y(8);
I cannot just simply reshpae the original x and y vectors to obtian the acsending order then to calculate f_new. That wont be right.

-Chen



From: Roger Stafford on
"Chen " <neversaynever(a)never.org> wrote in message <i41erd$3rj$1(a)fred.mathworks.com>...
> Hi Roger,
>
> Thanks agian for your reply. Sorry I didn't make it clear. I will just give numbers this time to try to make it clear.
>
> Given: 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];
> There is a function which is x and y dependent and it's defined as f = x.*y.
> So f = [ 0.5 1.2 2.1; 16 15.6 14; 8 14.4 22.4];
>
> I actually want to "reshape" this 3 X 3 matrix to a new 9 X 9 matrix with each element f_new(i,j) w. r .t (new_x(j) ,new_y(i))
> new_x = [1, 2, 3, 4, 6, 8, 10, 13, 16]; %% "reshape" vector x in an acsending order.
> new_y = [0.5, 0.6, 0.7, 1, 1.2, 1.4, 2, 2.4, 2.8]; %% in acsending order too.
>
> f_new = [0.5 0 0 0 0 0 0 0 0;
> 0 1.2 0 0 0 0 0 0 0;
> 0 0 2.1 0 0 0 0 0 0;
> 0 0 0 0 0 0 0 0 16;
> 0 0 0 0 0 0 0 15.6 0;
> 0 0 0 0 0 0 14 0 0 ;
> 0 0 0 8 0 0 0 0 0 ;
> 0 0 0 0 14.4 0 0 0 0;
> 0 0 0 0 0 22.4 0 0 0]
> for example: f_new(1,1) = new_x(1)*new_y(1);
> f_new(4,9) = new_x(9)*new_y(4);
> f_new(8,5) = new_x(5)*new_y(8);
> I cannot just simply reshpae the original x and y vectors to obtian the acsending order then to calculate f_new. That wont be right.
>
> -Chen
- - - - - - - -
I think I understand what you want now, Chen. At least the following matches the example you have given.

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);

In ff the elements of f are placed along the diagonal and the sorting permutations q and p are applied to its rows and columns to arrive at f_new.

Roger Stafford
From: Chen on
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';