Hi everybody. I have two problems. I think they are simple to solve but I cannot at this time!!!, I don't know how!!!
First problem: I constructed a matrix dkc of size (1057 X 11), and many zeros inside. I used this code to remove zeros:
for g=1:11
new_variable = dkc( dkc(:,g)~=0, 2);
end
but I got the resulted matrix in only one column. I want the matrix as it is, 2D with 11 columns, without zeros in the array.
Second problem I have, when I have another column say s with a different length than the previous matrix dkc. Can I check which column in dkc is nearest in its elements with this elements of s? and how? And which method will use?
Thanks for all, and really I need urgently these solutions to complete my research.

 采纳的回答

dpb
dpb 2014-3-20
编辑:dpb 2014-3-20
for g=1:11
new_variable = dkc(dkc(:,g)~=0, 2);
end
A) The above replaces new_variable each pass through so what you're left with is the last column for the logical addressing array for the row selection and the second column from the fixed 2.
B) Matlab cannot store "jagged" arrays--to actually not have anything as a placeholder for the zeros the only way you can do this is with a cell array. Or, if you just need an indicator consider substituting NaN -- certain Matlab functions are enabled that they will skip those elements altho certainly that's not universal.
To build the jagged array via your loop solution,
nv=cell(1,11); % an empty cell array of 11 columns
for i=1:11
nv(i)={dkc(dkc(:,i)~=0,i)};
end
NB the curlies "{}" to turn the double vector into a cell array

8 个评论

Thank you very much, it is working; but how can i assign the nearest cell from these with the another cell S (of another external unequal array) say 705 x 1, to get which one of them will be nearest. Also what is suitable method to get the best matching?
Thanks again for your great cooperation
Or, matching with another matrix of other cells to get which are nearest to these cells in dkc (one cell by one) or value by value.
Hard to say what would be "best" matching if they're of noncommensurate length -- what would you define as "matching"? Nearest average, minimum maximum difference, ...?
Many thanks for your response, actually, i got an idea, that is extending the shorter array of data or shrinking the longer one to be equal then i can match, but i don't know how i can do this. What is your opinion about this idea? suggest one.
Well, either concatenate to the shorter or select from the longer the subset. The latter would be sotoo
minRows = min(size(x,1), size(y,1)); % minimum rows in the two
x = x(1:minRows, :);
y = y(1:minRows, :);
By this way, will we neglect some elements from the longer to be shorter?And these values eliminated (or cutting) from the end of longer array will not affect on the array??
Yes, the above selects the first N rows where N is the min(size(...,1)) for the two. Yes, it therefore ignores the remainder after that in the longer and they're gone forever into the bit bucket in the sky...
Whether that's an acceptable result on the array or not depends entirely on what your application needs -- we can't answer that, only you can.
Thank you so much for your help, great cooperation, your time, and efforts.

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Logical 的更多信息

标签

尚未输入任何标签。

Community Treasure Hunt

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

Start Hunting!

Translated by