How to run the following code for n number of rows and m number of columns.
3 次查看(过去 30 天)
显示 更早的评论
A=[ 1 2 3 4 5;
11 12 13 14 15;
6 7 8 9 10;
21 22 23 24 25;
26 27 28 29 30];
B=[0 0 41 0 0;
45 0 0 0 0;
0 43 0 0 0;
0 0 0 42 0;
0 0 0 0 44];
C_after_step_1=cluster_rows(A,B,[1 4])
% C_after_step_1=cluster_rows(B_part)
C=cluster_rows(A,C_after_step_1,[2 3 5])
% C=cluster_rows(A,C_after_step_1)
calling function
function C=cluster_rows(A,B,rows)
%extract the parts of the matrices
A_part=A(rows,:);
B_part=B(rows,:);
%sum along the the vertical axis to get indices for the non-0 columns
non_0=sum(B_part);
%Repeat the vector back to the same size as the partial matrix. It will be
%converted to a logical later on.
non_0=repmat(non_0,length(rows),1);
%create a copy of B, as that is the basis for C
C=B;C_part=B_part;
%for all non-zero columns, replace the 0 positions with the values from A
C_part(non_0 & C_part==0)=A_part(non_0 & C_part==0);
%paste the edited partial matrix back
C(rows,:)=C_part;
end
15 个评论
Rik
2018-1-7
I don't really feel motivated to put in a lot of work to answer this question. A few questions back, you (or Jaah Navi) had some help how to generate random partitions of your matrix. Is that your goal here as well?
You can have this structure in a loop by doing something like this:
C=B;
for N=1:k
rows=[];%insert some code here that will get the row indices
C=cluster_rows(A,C,rows);
end
Prabha Kumaresan
2018-1-7
Could you please help me to do it as i was new to matlab and i am learning it.
Rik
2018-1-7
Sure. First describe in a clear manner what are the requirements for the grouping. Is there some method to the specific indices? Or to the group size?
And if I answer this and it solves your problem, are you going to accept my answer?
Prabha Kumaresan
2018-1-7
编辑:Walter Roberson
2018-1-7
If your answer solves my problem i will definitely accept it.There will be two matrix A and B where the size of A is rand(n,m) and B is [diag(1:n),diag(n+1:m)].Grouping of rows in B can be done in a random manner.There can be n number of groups and in each group there can be n number of rows under the condition if one row present in one group should not be present in other group.After grouping the rows in B,the rows present in the corresponding group should compare with each other to check whether they have non zero values in the same place if not they need to take value from A.
Rik
2018-1-7
As you have been told numerous times now, don't use the code layout for normal text. It makes it more difficult to understand you.
jaah navi
2018-1-10
I would like to check a small clarification with respect to the coding
A=[ 1 2 3 4 5;
11 12 13 14 15;
6 7 8 9 10;
21 22 23 24 25;
26 27 28 29 30];
B=[0 0 41 0 0;
45 0 0 0 0;
0 43 0 0 0;
0 0 0 42 0;
0 0 0 0 44];
C_after_step_1=cluster_rows(A,B,[1 4])
% C_after_step_1=cluster_rows(B_part)
C=cluster_rows(A,C_after_step_1,[2 3 5])
% C=cluster_rows(A,C_after_step_1)
calling function
function C=cluster_rows(A,B,rows)
%extract the parts of the matrices
A_part=A(rows,:);
B_part=B(rows,:);
%sum along the the vertical axis to get indices for the non-0 columns
non_0=sum(B_part);
%Repeat the vector back to the same size as the partial matrix. It will be
%converted to a logical later on.
non_0=repmat(non_0,length(rows),1);
%create a copy of B, as that is the basis for C
C=B;C_part=B_part;
%for all non-zero columns, replace the 0 positions with the values from A
C_part(non_0 & C_part==0)=A_part(non_0 & C_part==0);
%paste the edited partial matrix back
C(rows,:)=C_part;
end
In this coding with respect to the command C(rows,:)=C_part,how can i view the C_part in the workspace.Could you please help me to view the C_part in the workspace.
Rik
2018-1-10
编辑:Rik
2018-1-10
That C_part variable is inside the cluster_rows function and is wiped after that. You could add an optional second output that contains only that part of the matrix, or use breakpoints. I don't think you are really understanding what is happening here, so I would advise you to do a crash course on Matlab. That should teach you in a structured way how workspaces are separated.
change this:
function C=cluster_rows(A,B,rows)
to this:
function [C,C_part]=cluster_rows(A,B,rows)
Now you can keep the use the same in your loop, or add the second output so you can see it in your workspace.
jaah navi
2018-1-15
I would like to check how do I need to calculate the throughput for the C_part with respect to equation
Throughput=sort(Bmax*log2(1+(p_fix*C_part)/(noise+(p_fix*C_part(s)))))
where Bmax=2000000; noise=1e-9; p_fix=0.01;
Rik
2018-1-15
I don't know what throughput means in your context, so I can't tell if you did that correctly. I can say to double check if you need matrix multiplication, or element-wise multiplication. The first is * and the second is .*
jaah navi
2018-1-17
with respect to your command change this: function C=cluster_rows(A,B,rows) to this: function [C,C_part]=cluster_rows(A,B,rows) could you please tell me how to vertically concatenate C_part so that I can see all C_part together in the same matrix.
采纳的回答
Rik
2018-1-7
%generate random data for A and B (replace this with your real data)
m=50;n=2*m;
A=randi(m*n,m,n);
%B=randi(m*n,m,n).*(rand(m,n)>0.9);
B=[diag(1:n),diag(n+1:m)];
unused_rows=1:m;C=B;
while ~isempty(unused_rows)
%This method of random selection will have a bias to start off with
%large groups and end with small ones. If this is a problem, you will
%have to devise a method yourself that results in a more stable
%distribution. Taking a square root and rounding the result already
%helps a bit to equalize group size.
n_rows=ceil(sqrt(randi(numel(unused_rows))));
rows=unused_rows(randsample(length(unused_rows),n_rows));
[~,idx]=find(ismember(unused_rows,rows));
unused_rows(idx)=[];
%If you want to see the rows that were grouped, uncomment the next two
%lines of code. (e.g. if you want to see the group size)
%fprintf('%02d ',rows)
%fprintf('\n')
%apply the row clustering
C=cluster_rows(A,C,rows);
end
18 个评论
Prabha Kumaresan
2018-1-7
Thanks.one small clarification.If i run the code how can i see the total result together in the command D(rows,:)=C_part
Rik
2018-1-7
That line is nowhere in either my or your code. If you are referring to the last line of the cluster_rows function, why would you want to? The result is saved in C anyway. But if you really insist, use disp(C_part) inside cluster_rows, or disp(C(rows,:)) at the end of the loop.
Prabha Kumaresan
2018-1-8
If i use disp(C(rows,:)) at the end of the loop it displays the result of the last for loop Cluster_rows.Actually what I need is I need to diaplay the result for all Cluster_rows together.Could you please help me on this.
Prabha Kumaresan
2018-1-8
It displays the C matrix.But i want to have C_part =
49 24 0 0 5 33 24 0 0 10
1 41 0 0 45 6 30 0 0 8
15 2 0 0 35 25 7 0 0 44
and
C_part =
0 0 41 4 0 0 0 35 9 0
0 0 3 48 0 0 0 8 25 0
together in a single matrix.
As i have used C_part in the function could you please help me to display it.
Prabha Kumaresan
2018-1-8
I would like to check the comment B=[diag(1:n),diag(n+1:m)] with respect to your coding.
I think in that line m and n are interchanged so it should be B=[diag(1:m),diag(m+1:n)].Could you please check it for me.
And similarly in the command line n_rows=ceil(sqrt(randi(numel(unused_rows))));
rows=unused_rows(randsample(length(unused_rows),n_rows)); whether it is m_rows.
Rik
2018-1-8
As to your first comment: as I told you, use disp(C_part) inside the cluster_rows function.
As to you second comment: yes, B=[diag(1:m),diag(m+1:n)] is correct, it was a typo on my part. The name of the n_rows variable doesn't matter, you can safely change it if you want to. It means the number of rows to be selected, which is the reason for using n instead of m in the name.
And why do you still use the code layout for text? Don't start text with two spaces. Edit your comment. You have been told numerous times. I will ignore the next comment that is formatted this way without reason.
jaah navi
2018-1-10
I would like to check with the following code
%generate random data for A and B (replace this with your real data)
m=50;n=2*m;
A=randi(m*n,m,n);
%B=randi(m*n,m,n).*(rand(m,n)>0.9);
B=[diag(1:m),diag(m+1:n)];
unused_rows=1:m;C=B;
while ~isempty(unused_rows)
%This method of random selection will have a bias to start off with
%large groups and end with small ones. If this is a problem, you will
%have to devise a method yourself that results in a more stable
%distribution. Taking a square root and rounding the result already
%helps a bit to equalize group size.
n_rows=ceil(sqrt(randi(numel(unused_rows))));
rows=unused_rows(randsample(length(unused_rows),n_rows));
[~,idx]=find(ismember(unused_rows,rows));
unused_rows(idx)=[];
%If you want to see the rows that were grouped, uncomment the next two
%lines of code. (e.g. if you want to see the group size)
%fprintf('%02d ',rows)
%fprintf('\n')
%apply the row clustering
[D,D_part]=cluster_rows(A,C,rows);
end
calling function
function [D,D_part]=cluster_rows(A,C,rows)
%extract the parts of the matrices
A_part=A(rows,:)
C_part=C(rows,:)
%sum along the the vertical axis to get indices for the non-0 columns
non_0=sum(C_part)
%Repeat the vector back to the same size as the partial matrix. It will be
%converted to a logical later on.
non_0=repmat(non_0,length(rows),1)
%create a copy of B, as that is the basis for C
D=C
D_part=C_part
%for all non-zero columns, replace the 0 positions with the values from A
D_part(non_0 & D_part==0)=A_part(non_0 & D_part==0)
%paste the edited partial matrix back
D(rows,:)=D_part
end
while i run the code it executes and it displays the last result of D_part in the workspace.How to view the D_part in the workspace for each time when the function is being called.Could you please help me on this.
Rik
2018-1-10
You are changing variable names left and right, without checking if what you are doing is correct. You changed C to D only in some places, which now means the code you pasted doesn't work anymore (although it does run without errors). Below I have fixed the code again by reverting most changes. You can see the C_part matrix as a cell array containing each step. It is overwritten in your code, which is why you can only see the last result.
m=50;n=2*m;
A=randi(m*n,m,n);
%B=randi(m*n,m,n).*(rand(m,n)>0.9);
B=[diag(1:m),diag(m+1:n)];
unused_rows=1:m;C=B;
C_part=cell(1);
while ~isempty(unused_rows)
%This method of random selection will have a bias to start off with
%large groups and end with small ones. If this is a problem, you will
%have to devise a method yourself that results in a more stable
%distribution. Taking a square root and rounding the result already
%helps a bit to equalize group size.
n_rows=ceil(sqrt(randi(numel(unused_rows))));
rows=unused_rows(randsample(length(unused_rows),n_rows));
[~,idx]=find(ismember(unused_rows,rows));
unused_rows(idx)=[];
%If you want to see the rows that were grouped, uncomment the next two
%lines of code. (e.g. if you want to see the group size)
%fprintf('%02d ',rows)
%fprintf('\n')
%apply the row clustering
[C,C_part{k}]=cluster_rows(A,C,rows);
end
calling function
function [C,C_part]=cluster_rows(A,B,rows)
%extract the parts of the matrices
A_part=A(rows,:);
B_part=B(rows,:);
%sum along the the vertical axis to get indices for the non-0 columns
non_0=sum(B_part);
%Repeat the vector back to the same size as the partial matrix. It will
%be converted to a logical later on.
non_0=repmat(non_0,length(rows),1);
%create a copy of B, as that is the basis for C
C=B;
C_part=B_part;
%for all non-zero columns, replace the 0 positions with the values from A
C_part(non_0 & C_part==0)=A_part(non_0 & C_part==0);
%paste the edited partial matrix back
C(rows,:)=C_part;
end
jaah navi
2018-1-11
编辑:jaah navi
2018-1-11
I would like to check with respect to the command line rows=unused_rows(randsample(length(unused_rows),n_rows)) which displays how many number of rows need to be considered for each run.As randsample is being used number of rows can be 1,2,3....But how to avoid the number of rows to be 1.It can be more than one but it should not be one.could you please help me on this.
jaah navi
2018-1-11
With respect to the command line rows=unused_rows(randsample(length(unused_rows),n_rows)); why the number of rows are 1 or 2 or 3.why its not taking more than 4.Could you please help me on this.
Rik
2018-1-11
Read the comments I wrote in my code, I did not write them for no reason.
%This method of random selection will have a bias to start off with
%large groups and end with small ones. If this is a problem, you will
%have to devise a method yourself that results in a more stable
%distribution. Taking a square root and rounding the result already
%helps a bit to equalize group size.
n_rows=ceil(sqrt(randi(numel(unused_rows))));
If you want n_rows not to be 1, adapt this line. I you want n_rows to also be able to be 4, adapt this line. n_rows controls how many values are selected from unused_rows. A word of warning: if it should never be 1, you need to have a check that you are not leaving 1 for the last loop.
jaah navi
2018-1-11
Ok.As I am learning matlab could you please help me to include the word of warning condition in the command line n_rows=ceil(sqrt(randi(numel(unused_rows)))).
Rik
2018-1-11
Sure. Start off by thinking in a structure way what should happen when. In other words, write psuedocode. Then convert that pseudocode into real Matlab. The first step you should be able to do on your own, the second you can ask follow up questions for on this forum.
tip: Write down explicitly what rules n_rows must obey.
Prabha Kumaresan
2018-1-19
I would like to check if
m=[10 20 30 40 50] and
n=[60 70 80 90 100]
how it can be done with respect to the below section
A=randi(m*n,m,n);
%B=randi(m*n,m,n).*(rand(m,n)>0.9);
B=[diag(1:n),diag(n+1:m)];
unused_rows=1:m;C=B;
while ~isempty(unused_rows)
%This method of random selection will have a bias to start off with
%large groups and end with small ones. If this is a problem, you will
%have to devise a method yourself that results in a more stable
%distribution. Taking a square root and rounding the result already
%helps a bit to equalize group size.
n_rows=ceil(sqrt(randi(numel(unused_rows))));
rows=unused_rows(randsample(length(unused_rows),n_rows));
[~,idx]=find(ismember(unused_rows,rows));
unused_rows(idx)=[];
%If you want to see the rows that were grouped, uncomment the next two
%lines of code. (e.g. if you want to see the group size)
%fprintf('%02d ',rows)
%fprintf('\n')
%apply the row clustering
C=cluster_rows(A,C,rows);
end
更多回答(0 个)
另请参阅
标签
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)