フィルターのクリア

STORAGE OF SELECTIVE VALUES OF SOME CELLS INTO A SINGLE MATRIX FROM TWO DIFFERENT MATRICES. Whats wrong with this?

1 回表示 (過去 30 日間)
%Matrices Given
A = [1 2 3 4 5;6 7 8 9 10;1 4 5 6 7;8 9 44 5 6;5 5 16 25 44];
B = [3 4 16 44 66;16 25 33 44 66;20 31 45 66 80;11 22 33 44 55;7 10 25 44 60];
%Storage of a Matrix
C = [A(:,1)<4 & A(:,1)>=2;B(:,1)>4 & B(:,1)<40;A(:,2)<10 & A(:,2)>7;B(:,2)>=25 & B(:,2)<50];
What i expected was?
C =
2
3
16
8
9
25
33
44
But all i get is zeros and ones, Why? is it not the right way?
  2 件のコメント
William Alberg
William Alberg 2020 年 5 月 15 日
"A(:,1)<4 & A(:,1)>=2" only creates a logical matrix. You need to use the logical matrix to select values. Something like "A(A(:,1)<4 & A(:,1)>=2,1)"
Sandip Ghatge
Sandip Ghatge 2020 年 5 月 15 日
I tried your suggestion, that just returns 1.

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

採用された回答

William Alberg
William Alberg 2020 年 5 月 15 日
編集済み: William Alberg 2020 年 5 月 15 日
Okay, you have made 2 errors:
  1. A(:,1) returns: [1;6;1;8;5]. I think you want it to return [1, 2, 3, 4, 5], which is A(1,:).
  2. "A(:,1)<4 & A(:,1)>=2" gives an index, you want the corresponding values, which is: "A(A(:,1)<4 & A(:,1)>=2,1)" (Note that error 1 isn't fixed here)
You will need to understand this, otherwise you will make the same error again.
Since this appears to be for a school assignment, i will only correct the first section, you will need to the others yourself
C = [A(1,A(1,:) < 4 & A(1,:) >= 2)];
  5 件のコメント
William Alberg
William Alberg 2020 年 5 月 16 日
I am a bit confused about what you are trying to achieve in the first example. And i also dont understand why the numbers have changed. i think you want it to be:
C = [A(A(:,1) < 6 & A(:,1) > 1,1)]
However, that does not give the result you desired in your first post.
Your second example has a mistake in the last 5 characters. You have "),1];". It needs to be ",1)];"
Again, that does not give the result you originally specified.
The code below is your original solution, with my first answer inserted. ( I changed the layout a bit)
C = [...
A(1,A(1,:) < 4 & A(1,:) >= 2)';...
B(:,1)>4 & B(:,1)<40;... % needs change
A(:,2)<10 & A(:,2)>7;... % needs change
B(:,2)>=25 & B(:,2)<50]; % needs change
Sandip Ghatge
Sandip Ghatge 2020 年 5 月 16 日
Sorry, for confusing you.
I have got my desired results, and have written such codes for both columns of A & B.
Thank you very much.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by