insertion and replacement of specific rows and columns
10 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
dpb
on 20 Mar 2014
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.
4 Comments
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.
More Answers (2)
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.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!