How do i do this?

1 次查看(过去 30 天)
Anita Osoh
Anita Osoh 2020-11-16
回答: Rik 2020-11-16
  • Check if SelectLogicalN( [-2, 3, 6; 5, 78, 44; 9, -3, -53;], [1, 0, 0; 0, 0, 1; 1, 1, 1;], 4) returns [-2; 9; -3; 44;].
  • Check if SelectLogicalN( [-9, 3, 6; 2, 67, 1; 5, -4, -17; ], [ 0, 0, 0; 0, 1, 1; 1, 1, 0; ], 3) returns [5; 67; -4].
  • Check if SelectLogicalN( [-1, 4, 7; -2, 5, 8; -3, 6, 9], [1, 1, 0; 1, 1, 0; 1, 1, 0;], 5) returns [-1; -2; -3; 4; 5].
  3 个评论
Anita Osoh
Anita Osoh 2020-11-16
oh my question is how do i change an array size after indexing it?
Rik
Rik 2020-11-16
Probably by indexing the result. What is it exactly you want to do? What is the input and what is the output?

请先登录,再进行评论。

回答(1 个)

Rik
Rik 2020-11-16
Assuming that the comments in your screenshot are from the instructor guiding you through the writing of the function: I disagree with the proposed structure. What should happen if a user decides to call SelectLogicalN(rand(10,3),false(10,3),100)? The proposed structure will lead to an error, instead of simply returning a smaller vector (which is what I personally would prefer). I think this function should either return a smaller array, or trigger a custom error, telling the user what went wrong.
As for helping you solve this homework question:
I think it is better to use the logical input to determine a vector of indices. There is a built-in function that will do exactly that for you. You can even pass in the maximum number of indices. Can you figure out which function I'm referring to? Which functions do you know that take a logical input and will return the positions of true elements?

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by