Adding random data to left and right of each element in a matrix

This is a change from my previous question with random elements instead of fixed elements
Take for example,
i have two 3x4 matrices, M=
1 2 3 4
5 6 7 8
9 10 11 12
and E which shows the number of elements to be added to matrix M, random from 1-4
4 2 1 2
2 3 4 1
1 3 2 3
The elements to be added are random numbers from 0.1 to 0.9, the result to be obtained is shown below
1.3 1.9 1.4 1.1 1 1.5 1.3 1.9 1.7 2.2 2.4 2 2.9 2.5 3.1 3 3.7 4.8 4.2 4 4.4 4.1
5.3 5.8 5 5.5 5.1 6.6 6.1 6.3 6 6.2 6.2 6.7 7.2 7.3 7.2 7.9 7 7.1 7.5 7.4 8.8 8 8.2
9.2 9 9.5 10.9 10.7 10.6 10 10.4 10.3 10.4 11.3 11.4 11 11.1 11.9 12.3 12.9 12.5 12 12.3 12.4 12.8
Next is how do i store this data since it no longer stays as a matrix and is it possible use indexing to find a certain value eg. find(M==2)

 Accepted Answer

M = [
1 2 3 4
5 6 7 8
9 10 11 12];
E = [
4 2 1 2
2 3 4 1
1 3 2 3];
%
foo = @(m,e) m+randi(9,1,e)/10;
fun = @(m,e) [foo(m,e),m,foo(m,e)];
C = arrayfun(fun,M,E,'Uni',0);
Giving the output in a cell array C, where each cell contains a vector based on one element of M, e.g. the first column of C:
>> C{:,1}
ans =
1.4000 1.3000 1.9000 1.3000 1.0000 1.9000 1.1000 1.3000 1.9000
ans =
5.6000 5.6000 5.0000 5.9000 5.5000
ans =
9.1000 9.0000 9.3000
>>
" how do i store this data since it no longer stays as a matrix..."
I showed you how to put this into a cell array which is the same size as your original matrices. Alternatively you could extract all of the numeric data and concatenate it into one long numeric vector.
"...and is it possible use indexing to find a certain value eg. find(M==2)"
That depends on how your decide to store it: storing in a cell array requires a different way of searching than if you use one numeric vector.

6 Comments

Thanks for the help again! Real MVP!
I have another question hope you don't mind answering,
If the data to be added are not random but a H matrix that is generated for each element in M
For eg.
For M(1,1), there are 4 elements to be added
the H matrix calculated is [0.2 0.8 0.3 0.5]
which will add to the 1 to give
[1.5 1.3 1.8 1.2 1 1.2 1.8 1.3 1.5]
How do i change this line : m+randi(9,1,e)/10;
for this result:
[1+H(4) 1+H(3) 1+H(2) 1+H(1) 1 1+H(1) 1+H(2) 1+H(3) 1+H(4)]
Thanks! but how do i only apply this
H=[0.2 0.8 0.3 0.5]
to only M(1,1) and for M(1,2), H would change to [10 20], H is calculated to be different for every element in M. I will try to store H into a cell array so i could
use H{1} for M(1,1)
and H{2} for M(1,2)
Use a cell array, add it as an input to cellfun. something like this::
H = {[0.2 0.8 0.3 0.5],[10,20],...}
M = [...];
fun = @(m,h) [fliplr(h)+m,m,m+h];
cellfun(fun,num2cell(M),H,'Uni',0)
Thanks! It works. Now to the indexing part,I will be posting a new question for this.

Sign in to comment.

More Answers (1)

Since the padding on either side of each element of M is determined by the value of the corresponding element of E, and the sum of the rows in E do not sum to a constant, the rows in the final result will probably not all be the same length. Thus you will need to use a cell array.
It will be very easy for you to do this yourself once you're read the FAQ on cell arrays: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F

2 Comments

Hi, i could try the second part but first, how do i go about the first step, to add the data into my current matrix?
You've accepted an answer so I guess you figured it out.

Sign in to comment.

Tags

Asked:

on 16 Nov 2017

Commented:

on 20 Nov 2017

Community Treasure Hunt

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

Start Hunting!