could anyone help me to solve the issue in the following code

2 ビュー (過去 30 日間)
jaah navi
jaah navi 2019 年 9 月 10 日
コメント済み: jaah navi 2019 年 9 月 10 日
code:
a=[0.0022;
0.0922;
0.0146;
0.2549];
[m,n]=size(a);
differences = bsxfun(@minus,reshape(a,m,1,n),reshape(a,1,m,n));
v = nonzeros(differences');
newmat = reshape(v,3,4)';
val = max(newmat');
V=val';
the above code executes and gives me the result.
but when i run the code below
a=[0.0022 0.0021;
0.0922 0.0938;
0.0146 0.0143;
0.2549 0.2509];
[m,n]=size(a);
differences = bsxfun(@minus,reshape(a,m,1,n),reshape(a,1,m,n))
v = nonzeros(differences');
newmat = reshape(v,3,4)'
val = max(newmat')
V=val'
I am error stating Error using '
Transpose on ND array is not defined. Use PERMUTE instead.
Error in (line 20)
v = nonzeros(differences');
Could anyone please help me on it.
  2 件のコメント
madhan ravi
madhan ravi 2019 年 9 月 10 日
Desired result?
jaah navi
jaah navi 2019 年 9 月 10 日
V=[ -0.0124 0.0122;
0.0900 0.0917;
0.0124 0.0122;
0.2527 0.2488]
This was the result with respect to matrix size of (4,2)

サインインしてコメントする。

採用された回答

Raj
Raj 2019 年 9 月 10 日
編集済み: Raj 2019 年 9 月 10 日
"Could anyone please help me on it" - You have not exactly mentioned what help are you looking for. Your error message is self explanatory. In first case, the differences is a 4x4 2D matrix and transpose works on that. In second case, the differences is a 3D matrix with dimensions as 4x4x2 so transpose will not work here and you have to use 'permute'. If how to use permute is your doubt then see details here. Any other specific issues here?
Use this for your desired result:
a=[0.0022 0.0021;
0.0922 0.0938;
0.0146 0.0143;
0.2549 0.2509];
[m,n]=size(a);
differences = bsxfun(@minus,reshape(a,m,1,n),reshape(a,1,m,n))
v1 = nonzeros(differences(:,:,1)');
v2 = nonzeros(differences(:,:,2)');
newmat1 = reshape(v1,3,4)'
newmat2 = reshape(v2,3,4)'
val = [max(newmat1');max(newmat2')]
V=[val']
  3 件のコメント
Raj
Raj 2019 年 9 月 10 日
In case of large number of columns, you can define some cell arrays and loops like this:
a=[0.0022 0.0021;
0.0922 0.0938;
0.0146 0.0143;
0.2549 0.2509];
[m,n]=size(a);
differences = bsxfun(@minus,reshape(a,m,1,n),reshape(a,1,m,n))
v=cell(1,n);
for ii=1:n
v(1,ii) = {nonzeros(differences(:,:,ii)')};
end
newmat=cell(1,n);
for ii=1:n
newmat(1,ii) = {reshape(cell2mat(v(1,ii)),3,4)'};
end
val=max((cell2mat(newmat(1,1)))');
for ii=2:n
val = [val ;max((cell2mat(newmat(1,ii)))')];
end
V=[val']
This may not be the best way but it'll work.
jaah navi
jaah navi 2019 年 9 月 10 日
thanks.It works.

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by