MATLAB Answers

0

How to apply a matlab code on a matrix if it already working on column vector.

M.S. Khan さんによって質問されました 2019 年 8 月 5 日
最新アクティビティ M.S. Khan さんによって コメントされました 2019 年 8 月 13 日
MM = [3 3 3; 3 3 3; 0 0 0;0 0 0;3 3 3;3 3 3;0 0 0;0 0 0;0 0 0;3 3 0;0 3 3;0 0 3;3 0 0;3 3 0;3 3 3;0 0 0; 0 0 0;3 3 3;0 0 0];
i want to get output matrix in this shape:
output=[3 3 3;3 3 3;3 3 3;3 3 3 ;3 3 3;3 3 3;4 4 4;4 4 4;4 4 4;3 3 4;4 3 3;4 4 3;3 4 4;3 3 4;3 3 3;3 3 3;3 3 3;3 3 3;0 0 0];
% --------------------------------------------------------------
% This coding is working nicely on a column vector M.
% How to apply it on a matrix MM as given above.
%--------------------------------------------------------------
M =[3,3,0,0,3,3,0,0,0,3,3,0,0,3,3,0,0,3,0]';
m = M;
m(m == 0) = nan;
%lo - define the indices of the values that need to be changed:
lo = fillmissing(m,'previous') == 3 & fillmissing(m,'next') == 3 & isnan(m);
% ii - assign numbers to intervals:
ii = cumsum([0;diff(lo)>0]).*lo;
%{
p - count the amount of each value of vector ii:
1 element - the number of zeros (0's);
2 element the number of values in the first interval.
3 element the number of values in the second interval, etc.:
%}
p = accumarray(ii(:)+1,1);
% Let a - the values for to be changed (vector with length equal max(ii), in our case - 4):
a = [3,4,4,3]'; % HERE fixed..
% Condition for intervals with length equal 1
a(p == 1) = 3;
% Replacement:
M(lo) = a(ii(lo));
Thanks for all cooperation.
Very thankful for support in advance.

  2 件のコメント

KSSV
2019 年 8 月 5 日
What is the criteria to replace some zeros with 3 and others with 4?
M.S. Khan 2019 年 8 月 5 日
If we compare M and output matrix.
suppose its a column vector: [3 00 3 0003 0003 00030]' => [3 3 3 3 4 4 4 3 4 4 4 3 3 3 3 3 0]'
pattern of filling:
-- Between first and next 3, repalce 0s with 4
-- Beteeen next 3 and next coming 3, replace 0s with 4
-- next 3 and next coming 3, replace 0s with 3

サインイン to comment.

2 件の回答

回答者: Andrei Bobrov
2019 年 8 月 12 日
編集済み: Andrei Bobrov
2019 年 8 月 12 日
 採用された回答

[r,c] = size(MM);
m = MM;
m(m == 0) = nan;
%lo -define the indices of the values that need to be changed:
lo = fillmissing(m,'previous') == 3...
& fillmissing(m,'next') == 3 & isnan(m);
% ii - assign numbers to intervals:
ii = cumsum([zeros(1,c);diff(lo)>0]).*lo;
%{
p - count the amount of each value of vector ii:
1 element - the number of zeros (0's);
2 element the number of values in the first interval.
3 element the number of values in the second interval, etc.:
%}
[y,x] = ndgrid(1:r,1:c);
p = accumarray([ii(:)+1,x(:)],1);
p = p(2:end,:);
% Let a -the values for to be changed (vector for each column
% with length equal max(ii), in our case - 4):
a = repmat([3,4,4,3]',1,c);
% Condition for intervals with length equal 1
a(p == 1) = 3;
% Replacement:
MM(lo) = a(ii(lo));

  1 件のコメント

M.S. Khan 2019 年 8 月 13 日
Thanks Dr. Andrei Bobrov
Really you are great mind. Prayers and good wishes for you.
Warm Regards

サインイン to comment.


回答者: Chidvi Modala 2019 年 8 月 8 日

I am assuming the length of vector MM is pxq
You can use the below code
for i=1:q
M=MM(:,i);
% insert your algorithm here
M(lo)=a(ii(lo));
MM(:,i)=M;
end

  4 件のコメント

M.S. Khan 2019 年 8 月 12 日
I am assuming the length of vector MM is pxq What’s PXQ?
M.S. Khan 2019 年 8 月 12 日
for i=1:q ===> what will be the length of q?
M.S. Khan 2019 年 8 月 12 日
MM = [3 3 3; 3 3 3; 0 0 0;0 0 0;3 3 3;3 3 3;0 0 0;0 0 0;0 0 0;3 3 0;0 3 3;0 0 3;3 0 0;3 3 0;3 3 3;0 0 0; 0 0 0;3 3 3;0 0 0];
q = length(MM)
for i=1:q
M=MM(:,i);
m = M;
m(m == 0) = nan;
%lo - define the indices of the values that need to be changed:
lo = fillmissing(m,'previous') == 3 & fillmissing(m,'next') == 3 & isnan(m);
% ii - assign numbers to intervals:
ii = cumsum([0;diff(lo)>0]).*lo;
%{
p - count the amount of each value of vector ii:
1 element - the number of zeros (0's);
2 element the number of values in the first interval.
3 element the number of values in the second interval, etc.:
%}
p = accumarray(ii(:)+1,1);
% Let a - the values for to be changed (vector with length equal max(ii), in our case - 4):
a = [3,4,4,3]'; % HERE fixed..
% Condition for intervals with length equal 1
a(p == 1) = 3;
% Replacement:
M(lo) = a(ii(lo));
MM(:,i)=M;
end
% ----------------------------------- -----------------------
% the lenght of q is undefined.
first we should define the length of q. then we can use loop, right.
could you please explain. regards

サインイン to comment.



Translated by