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

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by prerna kaul
on 20 Mar 2014

hello All...

i am a fresh matlab student and i am suffering with the use of **permute,reshape,repmat,transpose** etc

**Please Note :** i have **any** m x n array and i have used examples to just clear my questions

also operations used here i.e square ,multiply ,subtract etc are used just to clearify that i wanna operate on elemenents of rows/columns

so please give me **general solutions** that can work with any array or any operations , **i apologize for so many questions** here

_ **problems :** _

**1.** i want to **add** "specific columns" in an m x n array

i.e

[1 3 2] [4 2 5]

i want to take **square of col 1** and **square-root of col 1** and append them as col 2 and 3 respectively, similarly for col 2 and every other columns (here i should end up with **9** columns) and no overwrite original columns

**2** . **Add** "specific rows" in array

i want to take 1st row and append the **same row** in next 2 rows , then 2nd row of "original" array and do the same .... if i take above ex then i want

1 3 2 1 3 2 1 3 2 4 2 5 4 2 5 4 2 5

**3.Replace** "specific rows" in array without overwriting

suppose i have

1 2 3 3 4 5 5 6 9

i want to take 1st row , multiply it with 2 and replace 2nd row take 1st row and subtract 1 frm it and replace 3rd row

1 2 3 2 4 6 0 1 2

*No products are associated with this question.*

Answer by dpb
on 20 Mar 2014

Accepted answer

Those are all subsets of addressing Matlab arrays and the use of the **colon** operator.

I recommend going to the "Getting Started" section of the documentation and working through the exercises to get a feel for how Matlab works.

For example, your first example/question starts out as (assuming existing array is *x* ) --

x=[x x(:,1).^2];

All of the rest are permutations of the above.

Show 1 older comment

dpb
on 21 Mar 2014

I gave you a specific example of one of your requests.

What do you mean "generalized version"? The syntax is general; it can be applied to whatever variable(s) you have. If you're talking about making variable names themselves as variables, the general answer there is "don't do that".

A specific example of what you're trying to do might help as at the moment it's unclear what the example above and the documentation is lacking. It may be that the reason you can't find the doc's for what you're asking for is because "Matlab doesn't work that way".

prerna kaul
on 21 Mar 2014

i have very large dataset in form of array

2417 x 103

and i want that conversion work for any array (any dimensions)

anyway **thank you** for answering

dpb
on 21 Mar 2014

*What* conversion? I'm sure there's a way to solve your problem but first we've got to get a clear definition of what the problem is as you see it.

What difference does it make what the size of the array is...what matters is what it is that you're after doing to it.

Your examples are all cases of modifying a given row/column and perhaps augmenting an array. All of those are handled by the colon notation in combination with various concatenation operations.

What's not clear is what you think is lacking.

PS. If it's related to wanting to do the same operation on more than one array, then you write a function that takes the target array as an argument and operates on it then returns the modified array to the target in the calling context. The function can determine the size dynamically and could also be given a vector of row/column targets on which to operate if the operation is the same but the location of an input column isn't always the same one.

To repeat, the problem is you haven't really told us enough to understand the context of the question/problem.

Answer by dpb
on 21 Mar 2014

*"... square of col 1 and square-root of col 1 and append them ... similarly for col 2 and every other columns..."*

OK, taking another stab at it with the additional conversation in mind perhaps I'm beginning to see what you're asking for. The way to generalize a given operation or set of operations is to write a function for it...

function a=example1(x) % Returns input array X augmented columnwise by each column squared and square root

[nr,nc]=size(x); % input rows, columns size a=[x zeros(nr, 2*nc); % load first columns and preallocate rest i1=nc+1; i2=i1+1; % initial target columns for i=1:nc a(i1:i2,:)=[a(:,i).^2 sqrt(a(:,i))]; i1=i2+1; i2=i1+1; end

To use save in an m-file *sample1.m* on the **matlabpath** and call.

a=[somearray of values]; newa=sample1(a);

and voila! Follow the same general idea for the rest.

The above is a "deadahead" solution using a loop that should be easy enough to follow what it does. There are more sophisticated ways to incorporate such things ( **accumarray** comes to mind as one, perhaps) but those are mere refinements of implementation if I've finally understood your fundamental question correctly.

Note the above is a level of enhancement above the simpler to code but performance-killing

for i=1:nc a=[a a(:,i).^2 sqrt(a(:,i))]; end

that relies on dynamic reallocation. This is ok for very small problems and learning but will be a real bottleneck as sizes of the arrays grows and should be avoided as a general practice except for the known cases that will always be small or not performance issues.

prerna kaul
on 21 Mar 2014

**thank you so much for your help :)**

and i think i have mentioned every possible detail in my question, but may be you may have misinterpretation

anyway i have to do some basic things only , not anything complicated

but i am very new here so i am taking more time to understand every small thing

Answer by prerna kaul
on 21 Mar 2014

Edited by prerna kaul
on 21 Mar 2014

ok i have solved my 2 questions

1.

[m,n]=size(X); Y=reshape([X;X.^2;sqrt(X)],m,n,3); Z=reshape(Y(:,:),m,[]);

2.

[m,n]=size(X); Y=reshape([X;X;X],n,m,3); Z=reshape(Y(:,:)',n,[])';

3. still experimenting.......

## 0 Comments