Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to generate such a data table?

Subject: How to generate such a data table?

From: Chen

Date: 12 Aug, 2010 15:55:23

Message: 1 of 9

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



 

Subject: How to generate such a data table?

From: Roger Stafford

Date: 12 Aug, 2010 16:46:21

Message: 2 of 9

"Chen " <neversaynever@never.org> wrote in message <i415ha$h7q$1@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

Subject: How to generate such a data table?

From: Chen

Date: 12 Aug, 2010 18:34:21

Message: 3 of 9

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


    

Subject: How to generate such a data table?

From: Roger Stafford

Date: 12 Aug, 2010 21:17:07

Message: 4 of 9

"Chen " <neversaynever@never.org> wrote in message <i41erd$3rj$1@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

Subject: How to generate such a data table?

From: Chen

Date: 13 Aug, 2010 00:14:23

Message: 5 of 9

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

Subject: How to generate such a data table?

From: Roger Stafford

Date: 13 Aug, 2010 00:31:23

Message: 6 of 9

"Chen " <neversaynever@never.org> wrote in message <i422ov$3u9$1@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

Subject: How to generate such a data table?

From: Chen

Date: 13 Aug, 2010 11:43:05

Message: 7 of 9


> 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

Subject: How to generate such a data table?

From: Roger Stafford

Date: 14 Aug, 2010 00:17:05

Message: 8 of 9

"Chen " <neversaynever@never.org> wrote in message <i43b49$c8l$1@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

Subject: How to generate such a data table?

From: Chen

Date: 16 Aug, 2010 11:40:30

Message: 9 of 9

Thanks a lot Roger. You always know the excellent tip to my code problem. This significantly reduce the running time for a huge data.

-Chen

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us