Hello everyone
My problem is as follows. I have a matrix like this for example:
A = [1 2 3 4 5 6 6 6 6;
2 4 3 5 4 6 7 7 7;
5 4 4 4 3 7 8 8 8;
2 1 3 3 3 3 3 3 5]'
Now i want to get a matrix which deletes the values which are similar (starting with the second) untill the end of the column. So columns 1, 2, 3 should delete some values while column 4 shouldn't delete any numbers.
At the end the matrix should look like this
A = [1 2 3 4 5 6 NaN NaN NaN;
2 4 3 5 4 6 7 NaN NaN;
5 4 4 4 3 7 8 NaN NaN;
2 1 3 3 3 3 3 3 5]'
I hope you get my problem. Thanks in Advance.

1 件のコメント

Peter Strassmann
Peter Strassmann 2020 年 10 月 19 日
編集済み: Peter Strassmann 2020 年 10 月 19 日
Be aware that NaN values are only defined for data with single or double precision! It is resets integers to zero, e.g. uint8, int8.
rng(1);
A=randi([0,8], [100,1], "int8");
%A=double(A);
A(A>2)=nan;
If you want to associate NaN values to such data, you have to convert them first (before the remapping) with single(A) or double(A) into values with the according precision.

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

 採用された回答

Stephen23
Stephen23 2017 年 4 月 13 日
編集済み: Stephen23 2017 年 4 月 14 日

1 投票

>> A = [1,2,5,2;2,4,4,1;3,3,4,3;4,5,4,3;5,4,3,3;6,6,7,3;6,7,8,3;6,7,8,3;6,7,8,5];
>> idx = cumprod(double(diff(flipud(A))==0),1);
>> idx(end+1,:) = false;
>> idx = logical(flipud(idx));
>> A(idx) = NaN
A =
1 2 5 2
2 4 4 1
3 3 4 3
4 5 4 3
5 4 3 3
6 6 7 3
NaN 7 8 3
NaN NaN NaN 3
NaN NaN NaN 5

5 件のコメント

Sascha  Winter
Sascha Winter 2017 年 4 月 13 日
Thanks for your answer. It works for this matrix but it doesn't work for columns where all positions are NaN, because i get the error code: Error using logical NaN's cannot be converted to logicals. Do you have an idea how to pass this problem?
Stephen23
Stephen23 2017 年 4 月 14 日
編集済み: Stephen23 2017 年 4 月 14 日
@Sascha Winter: how would you get a complete column of NaNs? As far as I can tell, if all elements in a column have the same value then following the description in your question "...starting with the second..." the first element would not be NaN. So how do you get a column of only NaNs?
>> A = [1,2,5,5;2,4,4,5;3,3,4,5;4,5,4,5;5,4,3,5;6,6,7,5;6,7,8,5;6,7,8,5;6,7,8,5]
A =
1 2 5 5
2 4 4 5
3 3 4 5
4 5 4 5
5 4 3 5
6 6 7 5
6 7 8 5
6 7 8 5
6 7 8 5
>> idx = cumprod(double(diff(flipud(A))==0),1);
>> idx(end+1,:) = false;
>> idx = logical(flipud(idx));
>> A(idx) = NaN
A =
1 2 5 5
2 4 4 NaN
3 3 4 NaN
4 5 4 NaN
5 4 3 NaN
6 6 7 NaN
NaN 7 8 NaN
NaN NaN NaN NaN
NaN NaN NaN NaN
Sascha  Winter
Sascha Winter 2017 年 4 月 14 日
You understood my the wrong way. In my original matrix (318x7690) i have columns where all values are NaN. If you take the cumprod of columns where all values are NaN it isn't possible. The columns in my original matrix contain a lot of NaN's so the cumprod function will not work. I hope you get what i mean. I give you an example of how my matrix probably looks like.
A = [123456666; 344456621; NaNNaN2456666; 122222NaNNaNNaN; NaNNaNNaNNaNNaNNaNNaNNaNNaN]'
My matrix then should look like this
A = [123456NaNNaNNaN; 344456621; NaNNaN2456NaNNaNNaN; 12NaNNaNNaNNaNNaNNaNNaN; NaNNaNNaNNaNNaNNaNNaNNaNNaN]'
So rows 3.4 and 6 wouldn't work with this solution. Thanks im Advance for your help.
Stephen23
Stephen23 2017 年 4 月 14 日
Try this:
A = [1,2,3,4,5,6,6,6,6; 3,4,4,4,5,6,6,2,1; NaN,NaN,2,4,5,6,6,6,6; 1,2,2,2,2,2,NaN,NaN,NaN; NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN]'
idx = diff(flipud(A));
idx = cumprod(double(idx==0|isnan(idx)),1);
idx(end+1,:) = isnan(A(1,:));
idx = logical(flipud(idx));
A(idx) = NaN
which outputs this:
A =
1 3 NaN 1 NaN
2 4 NaN 2 NaN
3 4 2 NaN NaN
4 4 4 NaN NaN
5 5 5 NaN NaN
6 6 6 NaN NaN
NaN 6 NaN NaN NaN
NaN 2 NaN NaN NaN
NaN 1 NaN NaN NaN
Sascha  Winter
Sascha Winter 2017 年 4 月 18 日
I think it works thanks a lot.

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

その他の回答 (0 件)

カテゴリ

タグ

質問済み:

2017 年 4 月 13 日

編集済み:

2020 年 10 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by