insertion and replacement of specific rows and columns
古いコメントを表示
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
採用された回答
その他の回答 (2 件)
dpb
2014 年 3 月 21 日
"... 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.
1 件のコメント
prerna kaul
2014 年 3 月 21 日
編集済み: prerna kaul
2014 年 3 月 21 日
prerna kaul
2014 年 3 月 21 日
編集済み: prerna kaul
2014 年 3 月 21 日
カテゴリ
ヘルプ センター および File Exchange で Creating and Concatenating Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!