0

Hi guys i need your help, so i have an array

a   b   c   n
1   1   2   4
1   3   2   6
1   6   0   7

and i want to create another array form each rows of my array, see picture below. enter image description here

I tried using this code:

assuming that my data is located at array M so,

for x=1:10
   d = M(:,4)/(M(:,1) + M(:,2) + M(:,3) + x)
end

but it doesn't give my desired output

in excel you just only write the equation and drag it down, in you will have the answer but i don't know how to do it in matlab, i think we could use for loop. thanks.

PLEASE SEE THE RED BOX THAT'S MY DESIRED OUTPUT

2
  • How do you get 10 values out of three rows? I assume the formula is repeated for each row? Do you also mean that your input data has ten rows even though you are only showing three? I would think so based on the code you posted...? And what is d in your code? Commented Feb 23, 2015 at 11:35
  • because your x = 1 to 10, meaning you will create another array of x having a size of 10x1. and another array of y which is proportional to the values of x, a,b,c, and n, so that in the end you will have a combine array of x and y Commented Feb 23, 2015 at 11:39

2 Answers 2

3

The equivalent in Matlab would be:

data = [...
1   1   2   4;
1   3   2   6;
1   6   0   7]

x = (1:10).';
f = @(t) data(t,4)./(data(t,1) + data(t,2) + data(t,3) + x )

y = [ x f(1) x f(2) x f(3) ]

or even simpler:

N = 10;
f = @(t) [(1:N).' data(t,4)./(data(t,1) + data(t,2) + data(t,3) + (1:N).' )]

y = [ f(1) f(2) f(3) ]

the number in f(...) always indicates which row, respectively which y e.g. y1, y2, etc. you are calculating for each column of the output. The brackets [...] are concatenating the result.

Be aware that you need to use the element-wise division operator ./


Generalized for an n x m sized input array, but assuming that the n-column is always the last one of your input Matrix:

N = 10;
f = @(t) [(1:N).' data(t,end)./(sum( data(t,(1:end-1))) + (1:N).' )]

y = cell2mat(arrayfun(f, 1:size(data,1),'uni',0))

But in this case you should think about, if a more vectorized approach like Divakar's answer might be more appropriate.


result:

y =

            1          0.8            1      0.85714            1        0.875
            2      0.66667            2         0.75            2      0.77778
            3      0.57143            3      0.66667            3          0.7
            4          0.5            4          0.6            4      0.63636
            5      0.44444            5      0.54545            5      0.58333
            6          0.4            6          0.5            6      0.53846
            7      0.36364            7      0.46154            7          0.5
            8      0.33333            8      0.42857            8      0.46667
            9      0.30769            9          0.4            9       0.4375
           10      0.28571           10        0.375           10      0.41176
Sign up to request clarification or add additional context in comments.

9 Comments

guys did you see my desired output? because base on your code it doesn't match my desired output.
@nobel well, than you should clarify you question. You just gave one relation y = n/(a+b+c+d) - that's what we posted. I have no idea what you want to tell us on the second screenshot. Please clarify
@nobel The only code you have posted, you said doesn't work. Put in words or equations what you intend to get.
base on the picture, it must generate 3 arrays, and each array have size of 10x2 containing x and y values.
@nobel so what is M? and what is the relation between y2 and M?
|
3

Vectorized approach to get the desired output with another good case for bsxfun to have the desired output for a generic m x n sized input array -

N = 10; %// Number of rows in the output
[m,n] = size(M) %// Get size

sum_cols = sum(M(:,1:n-1),2) %// sum along dim-2 until the second last column
sum_firstN = bsxfun(@plus,sum_cols,1:N) %// For each column-sum, add 1:N
out1 = bsxfun(@ldivide,sum_firstN,M(:,n)).'%//'# elementwise divide by last col
out = [repmat([1:N]',1,n); out1] %//'# Concatenate with starting columns of 1:N
out = reshape(out,N,[]) %// Reshape into desired shape

Code run for given 3 x 4 sized input array -

out =
    1.0000    0.8000    1.0000    0.8571    1.0000    0.8750
    2.0000    0.6667    2.0000    0.7500    2.0000    0.7778
    3.0000    0.5714    3.0000    0.6667    3.0000    0.7000
    4.0000    0.5000    4.0000    0.6000    4.0000    0.6364
    5.0000    0.4444    5.0000    0.5455    5.0000    0.5833
    6.0000    0.4000    6.0000    0.5000    6.0000    0.5385
    7.0000    0.3636    7.0000    0.4615    7.0000    0.5000
    8.0000    0.3333    8.0000    0.4286    8.0000    0.4667
    9.0000    0.3077    9.0000    0.4000    9.0000    0.4375
   10.0000    0.2857   10.0000    0.3750   10.0000    0.4118

8 Comments

it must generate 3 arrays, and each array have size of 10x2, containing x and y values
@nobel you just gave one equation for y and no equations for y2 and y2 so how should we know the relations?
sir y is general equation for y1,y2,and y3: for example: y1 = n1/(a1 + b1 + c1 + x1), and so on...
yes this what the desired output i want, thanks a lot, sorry for my wrong statement of my problem .
@thewaywewalk Guys, Well done for getting that right. I know I'm thick but I still haven't understood the problem...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.